Arrays. “Lists” of Variables Sometimes, it becomes necessary to order variables in a list: Temperature on Sept 1 = 90 Temperature on Sept 2 = 87 Temperature.

Slides:



Advertisements
Similar presentations
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Advertisements

CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
Arrays Chapter 6 Chapter 6.
Programming with Collections Collections in Java Using Arrays Week 9.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
CS 106 Introduction to Computer Science I 02 / 22 / 2008 Instructor: Michael Eckmann.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 02 / 20 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
CS 106 Introduction to Computer Science I 10 / 09 / 2006 Instructor: Michael Eckmann.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
BUILDING JAVA PROGRAMS CHAPTER 7 Array Algorithms.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
Java Unit 9: Arrays Declaring and Processing Arrays.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Lists in Python.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Arrays and Strings. Why? Consider a class of 30 students, each has a score for hw1  Do we want to have 30 variables with different names?  Would it.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Arrays Character Arrays and Strings Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Introducing Arrays in C. PURPOSE: Storing multiple data items under the same name Example:  Salaries of 10 employees  Percentage of marks of my dear.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
FP201 - PROGRAMMING FUNDAMENTALS Unit Understand the use of array PREPARED BY: MAZNAH AHMAD, JTMK PSIS.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Multiple Items in one Data Object Arrays are a way to store more than one piece of data in a data object, provided that all the data is of the same type.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Prepared by Andrew Jung. Accessing Pointer Data Pointer can be used to access the contents of an array Look at the following syntax: /* Declaration and.
Program: Shape Area Print the area for various shape types using the respective formulas.
Yong Choi School of Business CSU, Bakersfield
March 29th Odds & Ends CS 239.
Java Lesson 36 Mr. Kalmes.
CS2011 Introduction to Programming I Methods (II)
Introductory Java Programming
Presentation transcript:

Arrays

“Lists” of Variables Sometimes, it becomes necessary to order variables in a list: Temperature on Sept 1 = 90 Temperature on Sept 2 = 87 Temperature on Sept 3 = 84 in order to spot trends.

Lists of variables All of the variables and values are of the same type – integers, strings, whatever. Their order is important. Their place in the list is important May be shortened as… Temp1 = 90 Temp2 = 87 Temp3 = 84

Track the order as a separate variable Called an “index”: Temp[1] = 90 Temp[2] = 87 Temp[3] = 84 x = 2 Temp[ x ] = ? Name [ index ] = value…. index can change!

Arrays A difficult concept at first : int z[5]; Means: z[0], z[1], z[2], z[3], z[4] are separate integer variables Starts at 0. An array with 5 elements is indexed from 0 to 4. in Java: int z[ ] = { 0, 0, 0, 0, 0 }; Or int z[ ] = new int[5];

Array example int x; int z[ ] = { 0, 1, 2, 3, 4, 5, 6, 1, 1, 1 }; // z[4]= ? 1! for (x = 0; x < 10; x = x+1) { z[x] = x; } What is the value of z[4]? 4! in fact… z[0] = 0 z[1] = 1 z[2] = 2... to z[9] = 9

Arrays do not have to be numbers int x; String myArray[ ] = new String[5]; myArray[0] = ”hello"; myArray[1] = ”we"; myArray[2] = ”have"; myArray[3] = ”a quiz"; myArray[4] = ”next week"; for (x=0; x <= 4; x=x+1 ) { System.out.println( myArray[x] ); }

What are arrays good for? – They are confusing just to read… int a[ ] = new int[ 5 ]; EmployeeInfo[ 1021 ] Temperature[ x ]

What are arrays good for? – They are hard to track… “context awareness” is difficult What’s going on in this code? int x, RunningTotal = 0; int Sum[ ] = new int[ 5 ]; for (x = 0; x <= 4; x++) { RunningTotal = RunningTotal + x Sum[ x ] = RunningTotal; }

Sum[0] = 0 Sum[1] = Sum[2] = Sum[3] = Sum[4] = = 10

What are arrays good for? Think of the use of lists, that are –ordinal (ordered in sequence) –item positions are important –lists can be anything: strings, names, numbers, ages, costs, addresses

Polygons! int xPoints[ ] = { 55, 67, 109, 73, 83, 55, 27, 37, 1, 43 }; int yPoints[ ] = { 0, 36, 36, 54, 96, 72, 96, 54, 36, 36 }; Polygon star = new Polygon(xPoints, yPoints, 10);

Using three arrays to keep information String lastName[ ] = new String[ 5 ]; String firstName[ ] = new String[ 5 ]; int age[ ] = new int[ 5 ]; e.g. lastName[ 0 ] = “Truman”; firstName[ 0 ] = “Harry”; age[ 0 ] = 175;

array items are correlated by index

continued lastName[ 1 ] = “Garcia”; firstName[ 1 ] = “Jerry”; age[ 1 ] = 55; lastName[ 2 ] = “Smith”; firstName[ 2 ] = “John”; age[ 2 ] = 24;

continued lastName[ 3 ] = “Mouse”; firstName[ 3 ] = “Mickey”; age[ 3 ] = 75; lastName[ 4 ] = “Buckley”; firstName[ 4 ] = “Mike”; age[ 4 ] = 39;

array items are correlated by index record table field

What is this? Information that is grouped by index Kept in arrays e.g. lastName[1], firstName[1], and age[1] go together to form one profile for person #1 Is a DATABASE. We’re creating our own database.

printing out the info – note index int index = 0; userInput = JOptionPane.showInputDialog(“Enter index: “); index = Integer.parseInt( userInput ); if (index >=0) && (index <=4) { System.out.println(“ Item: ” + index ); System.out.println(“First Name: ” + firstName[ index ]) ; System.out.println(“Last Name: ” + lastName[ index ] ); System.out.println(“Age: ” + age[ index ] ); }

just imagine… Each array contains thousands of items More arrays to hold soc. sec. #, birthdate, pay scale, years of service Program ways to enter data as well as display. A full-scale Database Management program.

Changing array values - age public static void changeAge( int indexToChange ) { int newAge; userInput = JOptionPane.showInputDialog(“Enter age: “); newAge = Integer.parseInt( userInput ); age[ indexToChange ] = newAge; } note: array index is a parameter passed into the method nothing is returned, array is changed directly within the method (why? because the arrays are public)

Changing array values – first name public static void changeFirstName( int indexToChange ) { userInput = JOptionPane.showInputDialog(“Enter First: “); firstName[ indexToChange ] = userInput; }

Changing array values – last name public static void changeLastName( int indexToChange ) { userInput = JOptionPane.showInputDialog(“Enter Last: “); lastName[ indexToChange ] = userInput; }

printing out the info – note index // get user request, exit after user enters 4 or greater int index = 0; do { userInput = JOptionPane.showInputDialog(“Enter index: “); index = Integer.parseInt( userInput ); System.out.println(“First Name: ” + firstName[ index ]) ; System.out.println(“Last Name: ” + lastName[ index ] ); System.out.println(“Age: ” + age[ index ] + “for index ” + x); } while (index < 4 )