Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.

Similar presentations


Presentation on theme: "CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the."— Presentation transcript:

1 CS 180 Recitation 7 Arrays

2 Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the basic way to store large numbers of values.

3 Array declaration Array declaration format [ ] Examples: int scores[]; double[] args;

4 Array Creation [ ] = new [size] int[] anArray; anArray = new int[10]; /*allocates memory for 10 integers */ float anotherArray[]; anotherArray = new float[10]; // array of 10 floats

5 Java Array Default Values After you instantiate an array, default values are automatically assigned to it in the following manner. byte – default value is zero short – default value is zero int – default value is zero long – default value is zero, 0L. float – default value is zero, 0.0f. double – default value is zero, 0.0d. boolean – default value is false. reference types – default value is null.

6 Accessing arrays Each value is accessed by specifying an integer subscript or index in brackets following the array name. float marks[] = new float[20]; marks[0], marks[1] …...... marks[19] The index for an array must be between 0 and (length of the array -1) The length of the array can be found using the property of the array, array_name.length

7 Arrays: Continued... It is possible to declare and initialize an array at the same time. int x[] = {0, 1, 2} x.length = 3 You can also initialize array values using a loop. int x[] = new int[3]; for(int i = 0; i < x.length; i++) { x[i] = i; }

8 public class Main { public static void main(String[] args) throws Exception { int[] array1 = {1,2,3,4}; int[] array2 = {1,2,3,4}; int[] array3 = {1,2,3}; System.out.println("Comparing array1 and array 2" + Arrays.equals(array1, array2)); System.out.println("Comparing array1 and array 3“ + Arrays.equals(array1, array3)); }

9 Result Is array 1 equal to array 2?? true Is array 1 equal to array 3?? false The Arrays class contains various methods for manipulating arrays (such as sorting and searching).

10 Array of Objects Similar to array of primitive types. Student students[] = new Student[3]; This creates an array of references to hold 3 Student objects. students[0] = new Student(); students[1] = new Student(); students[2] = new Student(); for(int i = 0; i < students.length; i++) { students[0] = new Student(); }

11 Searching through the array int cnt = 0; for(int i = 0; i < a.length; i++) { if(a[i] == 100) { cnt++; } for(int i = 0; i < students.length; i++) { if(students[i].name.equals(“Tom”)) { System.out.println(“Tom ID: “ + students[i].id); }

12 Deleting an array entry Deleting a Student Tom Object. for(int i = 0; i < students.length; i++) { if(students[i].name.equals(“Tom”)) { students[i] = null; } Deleting a array entry doesn't actually shrink the array.

13 public class Example { public static void main(String[] args) { int x[] = {0, 1, 2, 3, 4, 5}; int median = 0; if(x.length %2 == 0) { median = (x[x.length/2] + x[(x.length/2)+1])/2; } else { median = x[x.length/2]; }

14 Using arrays in methods public void changeValue(float[] numbers) { } float x[] = new float[5]; changeValue(x); //Method Call During the method call, the value of the argument (which is an address) is copied to the method parameter (numbers). Both numbers and x now point to the same address.

15 Two Dimensional Arrays 2-dimensional arrays are usually represented with a row-column "spreadsheet" style. Assume we have an array, a, with two rows and four columns. int[][] a = new int[2][4]; a[0][0]a[0][1]a[0][2]a[0][3] a[1][0]a[1][1]a[1][2]a[1][3]

16 Declaration [][] Creation =new [ ][ ] int x[][]; x = new int[10][12]; // table of 10 rows and 12 columns

17 Searching a two dimensional array for (int i = 0; i < scoreTable.length; i++) { for (int j = 0; j < scoreTable[i].length; j++) { if(scoreTable[i][j] == 100) { System.out.println(“Score is 100”); break; }

18 public class Main { public static void main(String[] args) throws Exception { int A[][] = new int[3][4]; int sum = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) sum = sum + A[i][j]; System.out.println(“Sum is “ + sum); }

19 Jagged Arrays 2D arrays need not be rectangular. You can create a 2D array with m rows and each row have different number of slots. subarrays may have different lengths. double scoreTable = new double[4][ ]; for (int i = 0; i < 4; i++) scoreTable[i] = new double [i + 1];


Download ppt "CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the."

Similar presentations


Ads by Google