Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays - The Movie8.6- 8.8 A Farelly Brothers Presentation. No. Not Really.

Similar presentations


Presentation on theme: "Arrays - The Movie8.6- 8.8 A Farelly Brothers Presentation. No. Not Really."— Presentation transcript:

1 Arrays - The Movie8.6- 8.8 A Farelly Brothers Presentation. No. Not Really.

2 8.6: Parallel Arrays Oftentimes, data occurs in pairs, such as storing someone’s name and birthday. For this, we use PARALLEL ARRAYS

3 8.6: Parallel Arrays String[] name = {”a”, “b”, “c”}; int[] age = {20, 21, 19}; String searchName; int corresponding age = -1; int i; searchName =.... //Person desired for (int i = 0; i < name.length; i++) { if (searchName.equals(name[i]) { correspondingAge = age[i]; break; } } if (correspondingAge == -1) System.out.println(searchName + “ not found.”); else System.out.println(”The age is “ + correspondingAge);

4 8.7: 2D Arrays Suppose I wanted to create the multiplication table. A one-dimensional array just wouldn’t cut it. So we move on to TWO dimensional arrays. Huzzah!

5 8.7: 2D Arrays Declaration and Instantiation: int [][] table = new int[4][5] What’d this do? Creates an 2D array with 4 rows and 5 columns.

6 8.7: 2D Arrays Watch out for those off by one errors! Four rows means 0-3. Five columns means 0-4 0,00,10,20,30,4 1,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33,4

7 8.7: 2D Arrays How do I stick stuff in? 77 = table[2][3] Set the value at position 2,3 to 77. 0,00,10,20,30,4 1,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33,4

8 8.7: 2D Array Madness! Monitors on. Write the code right now to create and display the multiplication table from 0-6. You have 5 minutes...

9 8.7: 2D Arrays Hopefully you made good use of for loops to create the array. Let’s look at two other valuable code fragments, shall we? I think we shall!

10 8.7: Sum the Elements int i, j; int sum = 0; for (i = 0, i < 4, i++) { for (j = 0; j < 5; j++) { sum += table[i][j]; } } Note: Could have used i < table.length and j < table[i].length

11 8.7: Sum the Rows int i, j; int rowSum = new int[4]; for (i = 0, i < abc.length, i++) { for (j = 0; j < abc[i].length; j++) { rowSum[i] += table[i][j]; } }

12 8.7: Variable Length Rows You can have each row be a different length (though this is uncommon). This creates a RAGGED ARRAY. int[][] table; table = new int[4][]; // table has 4 rows table[0] = new int[6]; //row 0 has 6 columns table[1] = new int[8] // row 1 has 8 columns

13 8.7: Last thing with 2D ALL of the elements of a 2D array must be of the same type!

14 8.8: Arrays and Methods We have the ability to pass arrays to methods. This can be bad if the method mishandles the array because the array is now permanently altered by the method. Even so, let’s do it! Try and stop me! Here we go! I mean it!

15 Just kidding. Gotcha.

16 8.8: Arrays & Methods Got you again! Of course we’re going to do it. Sum the elements: int sum(int[] a) { int i, result = 0; for (i = 0; i < a.length; i++) result = += a[i]; return result; }

17 8.8: Using array method Example: int[] array1 = {4, 8, 13, 17, 6} int[] array2 = {72, 4, 54}... if(sum(array1) > sum(array2))....

18 8.8: Search Method int search (int[] a, int searchValue){ int location, i; location = -1; for (i = 0; i < a.length; i++){ if (a[i] == searchValue){ location = i; break; } } return location; }

19 8.8: Sum the rows int[] sumRows (int[][] a){ int i, j; int[] rowSum = new int[a.length]; for (i = 0; i < a.length; i++){ for (j = 0; j < a[i].length; j++){ rowSum[i] += a[i][j]; } } return rowSum; }

20 8.8: Using Row Sum int[][] twoD = {{1,2,3,4}, {5,6}, {7,8,9}}; int[] oneD; oneD = sumRows (twoD); oneD now references the array created and returned by the method sumRows. It equals {10, 11, 24}


Download ppt "Arrays - The Movie8.6- 8.8 A Farelly Brothers Presentation. No. Not Really."

Similar presentations


Ads by Google