Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be."— Presentation transcript:

1 ©2004 Brooks/Cole Chapter 8 Arrays

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be treated similarly Three Lists of Items

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating Arrays An array is an indexed collection of data values of the same type. Arrays are reference types (like objects) Three steps to creating an array – Declaration creates a reference variable – Use new to allocate memory – Assign values to individual elements

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays are like Objects Declaring an array allocates memory for a reference –The declaration does NOT allocate memory for the array data The result of the declaration double prices[];

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Allocating Memory for Array The Results of the Allocation price = new double[6]; –All elements are initialized to 0

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Example: A List of Prices Declaration double[] prices; Memory Allocation prices = new double[6] Assigning values prices[0] = 10.96; prices[1] = 6.43; prices[2] = 2.58; prices[3] = 0.58; prices[4] = 12.27; prices[5] = 6.39;

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Array Allocations int [] grade = new int[5]; char [] code = new char[4];

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Allocation You don't have to use a literal value for the array size –You can use any integer expression Examples int [] grade = new int[NUMELS]; int [] grade = new int[ size * 2];

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Size Once memory is allocated, the array has a fixed size Every array has a variable named length associated with it –length contains the number of elements for which memory was allocated –get the length using arrayName.length

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Access to Array Elements Use [ ] to access individual elements of an array –Index can be any integer expression –Individual elements can be used anywhere that a value of the base type can be used –Indexes go from 0 to one less that the length of the array

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Element Access Example grade[3]

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Declare and allocate memory for the following –a list of 100 integer years –a list of 6 character codes –a list of 32 floating point velocities Access the 1st, 3rd and 7th elements of the arrays above

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Practice What elements are output by the following for (m=1; m<=5; m++) System.out.println( a[m] + " "); for (int k=1; k< 5; k=k+2) System.out.println( a[k] + " ");

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Initializers You can initialize the elements of an array when you declare it int [] grade = {98, 87, 92, 79, 85}; char[] code = {'s', 'a', 'm', 'p', 'l', 'e'}; The size of the array is determined by the number of elements in the initializer list. – grade.length is 5 elements, code.length is 6

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 List of Prices with Initializer Declaration with initializer double[] prices = {10.96, 6.43, 2.58, 0.86, 12.27, 6.39}; The array has 6 elements – prices.length will be 6

16 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Practice Write array declarations with initialization for the following –integer grades 89, 75, 82, 93, 78, 95, 81 –floating point temperatures 78.2, 69.6, 68.5, 83.9, 55.4 –character menu choices p, a, c, w, m, q What is the length of each of the arrays?

17 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Arrays Once you have created an array, you can access the elements in any order Use loops to process all elements of the array in the same way –input values for each element –modify each element in the same way –print each element out

18 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays of Objects Declaration works the same as for arrays of primitive values Memory is allocated to store a reference to the array data String [] names; String names[];

19 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Allocating Memory for an Array of Strings Instantiation creates an array of references names = new String[4] Each element of the array has to be instantiated individually names[0] = "Joe"; names[1] = new String("Harriet");

20 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Assignment of Values Creates Actual Array Objects

21 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using arrays with methods Arrays can be used as parameters for methods –location of the array is copied into the method parameter variable –the contents of the array can be modified by the method An array can be returned by a method –the location of the array is returned

22 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays as Method Parameters Only one array exists –method gets a copy of the location

23 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays as method arguments The Location of the Array is Passed

24 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Write the header for a method called sort that takes an array of double values as a parameter and returns nothing (modifies the existing array) Write the header for a method called average that takes an array of doubles as a parameter and returns a double Write the header for a method called readArray that takes no parameters and returns an array of integers

25 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 2D Arrays What if we need to make a table instead of a list? –We can make arrays with two indexes Think of 2D array as an array whose elements are arrays

26 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating 2D Arrays Declaration int [][] table Instantiation table = new int[3][4]; Declaration with initialization int[][]table2 = {{8, 16, 9, 52}, {3, 15, 27, 6}, {14, 25, 2, 10}};

27 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Each Array Element Is Identified by Its Row and Column Position Access row 1 with table[1] Access element 3 of row 2 with table[2][3]

28 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Write declaration and allocation statements for –an array of integers with 6 rows and 10 columns –an array of doubles with 10 rows and 25 columns Write loops needed to add two 2x3 element arrays on integers and put the results in a new array.

29 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Representation of a Three-Dimensional Array

30 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Higher Dimensional arrays are also possible Need one index for each dimension For n dimensions, need n nested loops to process all elements Higher dimensional arrays are harder to visualize

31 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Deep Copy After the Call to System.arraycopy() newnums = System.arraycopy(nums) –This method creates a new array and copies the values from the original

32 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Result of a Shallow Copy newnums = nums


Download ppt "©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be."

Similar presentations


Ads by Google