Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.

Similar presentations


Presentation on theme: "Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to."— Presentation transcript:

1 Introducing Arrays

2 Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to create an application to track student progress in a class with 50 students? Creating 50 variables will prove inefficient …

3 Introducing Arrays  An array might prove a better solution to my problem!  An array is a data structure that contains related items that are of the same data type and where each item in the array shares the same name.  In memory, array values occupy contiguous locations.

4 Multiple Variables in Memory stu1stu2stu3stu4stu5 stu6 stu7 stu8 stu9 stu10stu11stu12 String stu1, stu2, stu3, stu4, stu5, stu6, stu7, stu8, stu9, stu10, stu11, stu12;

5 Printing all the students System.out.println( stu1 ); System.out.println( stu2 ); System.out.println( stu3 ); System.out.println( stu4 ); System.out.println( stu5 ); System.out.println( stu6 ); System.out.println( stu7 ); System.out.println( stu8 ); System.out.println( stu9 ); System.out.println( stu9 ); System.out.println( stu10 ); System.out.println( stu11 ); System.out.println( stu12 ); // How many lines if I wanted to print 200 students?

6 An Array in Memory fopStu String[ ] fopStu = new String[12]; fopStu[0]fopStu[1]fopStu[2]fopStu[3] fopStu[4]fopStu[5]fopStu[6]fopStu[7]fopStu[8]fopStu[9]fopStu[10]fopStu[11] Reference to the Array Array

7 Printing all the students in array for(int index = 0; index < 12; index++) { System.out.println( stu[index] ); } // How many lines if I wanted to print 200 students?

8 Creating an Empty Array in Two Steps A previous slide showed the line of code: String[ ] fopStu; This line is an example of how to create a reference to an array of Strings. This does not create the array object. To create the array we need the following line of code: fopStu = new String[12]; This line is an example of how to create an Array of Strings references. THERE ARE NO STRINGS. Just 12 adjacent memory locations waiting to point at Strings

9 Creating an Empty Array in a Single Step In general, to create an array of any type, you use the following: Create a reference assign it an Array object. [ ] = new [ ]; Therefore, if you wanted to create an Array to hold 15 Robots, you would type the following: Robot[ ] myRobots = new Robot[15]; This will create a reference to a Robot array that CAN hold 15 individual robots. After this code, NO ACTUAL Robots exist. There are just 15 Robot references in side of the Array.

10 Creating an Empty Array in Two Steps In some cases you may want to create the array reference, separately from the actual array. You can break the statement into two parts. Robot[ ] myRobots = new Robot[15]; would become: Robot[ ] myRobots; myRobots = new Robot[15]; //myRobots refers to an array that can hold 15 Robots Since arrays are object, the reference could later be used to “point” to a new array. In other words, the reference could be reassigned. myRobots = new Robot[25]; //myRobots now refers to an new array that can hold 25 // Robots the old Array and all its contents are now gone

11 Assigning Values to an Array  To assign a value to an array (after it has been created), you need to call the array by its name and then provide a subscript to store the new value (the index number goes in a pair of square brackets): String [] fopStu = new String [12]; String [] fopStu = new String [12]; fopStu[4] = "Jacob"; fopStu[5] = “Sally”; Robot [] myRobots = new Robot[4]; Robot [] myRobots = new Robot[4]; myRobots[3] = new Robot(1,1,North,0);

12 The array length field  Once an array is created, its size is fixed. It cannot be changed. The argument in the [ length ] fixes the size of the array when it is created.  We can find how many elements an array has by accessing the length field.  You can find its size of an array int numStu = fopStu.length;  length is always a nonnegative integer value: Based on previous slides, this would assign the value 12 into the variable numStu.

13 An Array in Memory(again) fopStu String[ ] fopStu = new String[12]; fopStu[0]fopStu[1]fopStu[2]fopStu[3] fopStu[4]fopStu[5]fopStu[6]fopStu[7]fopStu[8]fopStu[9]fopStu[10]fopStu[11] Note that each “cell” of the array has a separate index value. The smallest index value will ALWAYS be 0, and the largest will ALWAYS be one less than the length of the array. If the length is 12, then the max index is 11.

14 Initializing Arrays int[] myList = new int[5]; for (int i = 0; i < myList.length; i++) myList[i] = i * i; myList[i] = i * i; Would create the following array: myList 014916 myList[0]myList[1]myList[2]myList[3]myList[4]

15 Initializing Arrays Robot[] myBots = new Robot[3]; for (int i = 0; i < myBots.length; i++) myBots[i] = new Robot(1,i+1,North,0); myBots[i] = new Robot(1,i+1,North,0); Would create the following array: myBots myBots[0]myBots[1]myBots[2] Robot @ (1,1) Facing North 0 Beepers Robot @ (1,2) Facing North 0 Beepers Robot @ (1,3) Facing North 0 Beepers

16 Array Elements & Subscripts  An element is an object referenced in an array. Java stores each element in a different position in an array.  We reference a position using a subscript (a.k.a. index).  To reference an element, we need to provide the array's name and the element's subscript: myBots[0].move();  Each element in a array behaves exactly like a reference of the same type.

17 Creating an Array with Initial Values When we are declaring an array, we can supply specific values in a comma-delimited list: String[ ] students = {"James","Ravi","Mary”,”Jackson“}; int[ ] stuIds = { 8752, 9062, 6087, 8745, 2354, 1298}; Robot [] robs = { new Robot(1, 1, North, 3), new Robot(1, 2, North, 3), new Robot(1, 2, North, 3), new Robot(1, 3, North, 3), new Robot(1, 3, North, 3), new Robot(1, 4, North, 3)}; new Robot(1, 4, North, 3)};

18 Creating an Array with Initial Values (con’t) double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

19 CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] myList; myList = {1.9, 2.9, 3.4, 3.5};

20 Accessing Array Values  To access a value from an array, call the array by name and indicate which subscript you want to retrieve (the index number goes in a pair of square brackets): String name = fopStu[3]; int id = stuIds[3]; Robot karel = myBots[2];

21 Copying Arrays A simple assignment cannot copy arrays. Since an array is an object, if you try to use the assignment operator (=) you will end up with two references to the same array. If myList is the following array: myList 014916 myList[0]myList[1]myList[2]myList[3]myList[4] then, int[] anotherName; anotherName = myList; will result in: myList014916 myList[0]myList[1]myList[2]myList[3]myList[4] anotherName anotherName[0]anotherName[1]anotherName[2]anotherName[3]anotherName[4] The statement: anotherName[3] = 5; would also effect myList[3]

22 Traversing Arrays  To traverse an array means to “visit” each cell in order, doing whatever the job may be.  for loops do a VERY good job of this. BeeperSweeper [] sweepers = new BeeperSweeper[9];... // code to initialize all 9 cells with sweeper in // appropriate locations... for(int i = 0; i < sweepers.length; i++) sweepers[i].task(); This code will invoke each sweepers task method, without having to name 9 different robots and use 9 lines of code to invoke the task methods. Would the for loop still work if the array were created and initialized with a different number of BeeperSweepers?

23 Sample Algorithms  Summing/accumulating  Finding largest  Finding smallest  Searching  Sorting by ….

24 Summary  Arrays are data structures that can hold multiple values at the same time.  The values in an array are called elements and they share the same name (the array's name), but are accessed through unique subscripts (indicies). continued …

25 Summary  Subscript (index) numbering always begins with zero and goes up to the length of the array-1.  The size of an array can be found by using the length field.  When arrays are created, they are initially empty. If you want them to hold something, YOU must put it in. continued …

26 Summary  for loops are very useful in traversing an array.  Arrays are object, and therefore cannot be duplicated with the assignment (=) operator.

27 Assignment Part 2 *  From the CarDealer Project, complete the Dealer class. You will need to implement the following: You will need to implement the following: public boolean find(Car c)public boolean find(Car c) public double getInventoryValue()public double getInventoryValue() public double getAverageCarValue()public double getAverageCarValue() public Car getMostValuableCar()public Car getMostValuableCar() public Car getLeastValuableCar()public Car getLeastValuableCar() public void sortCarsByYear()public void sortCarsByYear() public void sortCarsByValue()public void sortCarsByValue() *See the IntroObjects PowerPoint for Asignment Part 1 Help for the above methods can be found in the ArrayAlgorithms PowerPoint


Download ppt "Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to."

Similar presentations


Ads by Google