180 likes | 274 Vues
8/7: Ch. 7: Arrays. What is an array? Declaring & allocating arrays Program of the day. Arrays: what are they?. a series of elements; a list. grocery list could be an array: milk bread eggs frozen pizza juice apples oranges Ramen noodles. Arrays: what are the positions?.
E N D
8/7: Ch. 7: Arrays • What is an array? • Declaring & allocating arrays • Program of the day
Arrays: what are they? • a series of elements; a list. • grocery list could be an array: • milk • bread • eggs • frozen pizza • juice • apples • oranges • Ramen noodles
Arrays: what are the positions? • every item has a place; a position number. place item 1. milk 2. bread 3. eggs 4. frozen pizza 5. juice 6. apples 7. oranges 8. Ramen noodles
Arrays: how are they numbered? • Java begins numbering at 0. place item 0. milk 1. bread 2. eggs 3. frozen pizza 4. juice 5. apples 6. oranges 7. Ramen noodles
Arrays: how are they named? • Java names the list and refers to the place of the items in square brackets after the list name. groceryList [0] = milk groceryList [1] = bread groceryList [2] = eggs groceryList [3] = frozen pizza groceryList [4] = juice groceryList [5] = apples groceryList [6] = oranges groceryList [7] = Ramen noodles
Arrays: what are the elements? • The individual items are called elements.The reference numbers are position numbers,or subscripts. groceryList [0] = milk groceryList [1] = bread groceryList [2] = eggs groceryList [3] = frozen pizza groceryList [4] = juice groceryList [5] = apples groceryList [6] = oranges groceryList [7] = Ramen noodles elements subscripts
Arrays: about the position numbers Subscripts must be integers or integer expressions. groceryList [0] = milk groceryList [1] = bread groceryList [2] = eggs groceryList [3] = frozen pizza groceryList [4] = juice groceryList [5] = apples groceryList [6] = oranges groceryList [7] = Ramen noodles
Arrays: function examples in Java • an array of integers: list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9 list.length = 7 list [0] + list [2] = 2 list [1 + 3] = 8
Declaring and allocating an array • to declare and allocate an array: int list[]; list = new int[7]; or int list[] = new int[7]; Another example: String args[] = new String[5]; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9
Declaring & initializing an array • to declare and initialize an array: • int list[] = {5,10,-3,-7,8,1,9}; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9
initializing elements of an array • to initialize an array element: • list[6] = 9; list [ 0 ] = 5 list [ 1 ] = 10 list [ 2 ] = -3 list [ 3 ] = -7 list [ 4 ] = 8 list [ 5 ] = 1 list [ 6 ] = 9
StudentPoll.java: pt. 1 //fig. 7.7: StudentPoll.java import javax.swing.*; public class StudentPoll { public static void main ( String args[] ) { int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8,10, 1, 6, 3, 8, 6,10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8,10}; int frequency[] = new int [ 11 ]; String output = "";
About StudentPoll.java: pt. 2 for ( int a = 0 ; a < responses.length; a++ ) ++frequency [ responses [ a ] ]; output += "Rating\tFrequency\n"; for ( int r = 1; r < frequency.length ; r++ ) output += r + "\t" + frequency[r] + "\n";
About StudentPoll.java: pt. 3 JTextArea outputArea = new JTextArea (11,10); outputArea.setText( output ); JOptionPane.showMessageDialog ( null , outputArea , "Student Poll Program" , JOptionPane.INFORMATION_MESSAGE ); System.exit ( 0 ); } }
About “.length” • length is actually an instance variable, indicating the number of elements in the array. • We don’t have to declare it separately – it’s an automatic part of an array that we declare.
Passing arrays to methods • Two ways: • passing the entire array as a whole • passing individual elements of the array • The method that modifies the array must have an appropriate input type (parameter). • Accepting an array: public void x ( int a[] ) • Accepting elements: public void y ( int c ) public void z ( String d )
Getting more experience with arrays • Pg 273: InitArray.java – declaring & allocating • Pg 274: InitArray.java – initializing to values • Pg 275: InitArray.java – using a for loop to set values • Pg 276: SumArray.java – using a for loop to add up elements of an array
Programs of the day • pg. 278: StudentPoll.java • After getting the program to run, modify the program to allow the user to input the array elements. • use a JOptionPane.showInputDialog to get the data from the user, and assign the CONVERTED value you get from there into the next array position. Use a for loop to provide the repetition and movement in the array position. • Pg. 284 PassArray.java