Java Programming: From Problem Analysis to Program Design, 4e Chapter 9 Arrays
Chapter Objectives <ul><li>Learn about arrays </li></ul><ul><li>Explore how to declare and manipulate data into arrays </l...
Chapter Objectives (continued) <ul><li>Discover how to pass an array as a parameter to a method </li></ul><ul><li>Discover...
Array <ul><li>Definition: structured data type with a fixed number of elements </li></ul><ul><li>Elements of an array are ...
One-Dimensional Arrays
One-Dimensional Arrays (continued)
One-Dimensional Arrays (continued) <ul><li>intExp  = number of components in array  >= 0 </li></ul><ul><li>0 <= indexExp <...
<ul><li>Array num: int[] num =  new  int[5]; </li></ul>Arrays
<ul><li>Array num: int[] num =  new  int[5]; </li></ul>Arrays (continued)
Array List
Array List (continued)
Array List (continued)
Array List (continued)
<ul><li>Specifying Array Size During Program Execution </li></ul>
<ul><li>The  initializer list  contains   values,  called initial values , that are placed between braces and separated by...
Array Initialization During Declaration (continued) <ul><li>When declaring and initializing arrays, the size of the array ...
<ul><li>Associated with each array that has been instantiated, there is a  public  ( final ) instance variable  length </l...
Arrays and the Instance Variable  length  (continued) <ul><li>This statement creates the array list of six components and ...
<ul><li>The value of  numList.length  is  10   </li></ul><ul><li>numList[0] = 5; </li></ul><ul><li>numList[1] = 10; </li><...
<ul><li>Loops used to step through elements in array and perform operations </li></ul><ul><li>int [] list =  new  int[100]...
Arrays (continued)  <ul><li>Some operations on arrays </li></ul><ul><ul><li>Initialize  </li></ul></ul><ul><ul><li>Input d...
Code to Initialize Array to Specific Value (10.00) for  ( int   index = 0; index < sales.length; index++) sales[index] = 1...
Code to Read Data into Array for  ( int   index = 0; index < sales.length;  index++) sales[index] = console.nextDouble();
Code to Print Array for  ( int   index = 0; index < sales.length; index++) System.out.print(sales[index] + &quot; &quot;);
Code to Find Sum and Average of Array sum = 0; for  ( int   index = 0; index < sales.length;  index++) sum = sum + sales[i...
Determining Largest Element in Array maxIndex = 0;  for  ( int   index = 1; index < sales.length;  index++) if  (sales[max...
Determining Largest Element in Array (continued)
Determining Largest Element in Array (continued)
Array Index Out of Bounds  <ul><li>Array in bounds if: </li></ul><ul><li>0 <= index <= arraySize – 1 </li></ul><ul><li>If ...
Declaring Arrays as Formal Parameters to Methods <ul><li>A general syntax to declare an array as a formal parameter  </li>...
The Assignment Operators and Arrays
The Assignment Operators and Arrays (continued)
The Assignment Operators and Arrays (continued)
Relational Operators and Arrays <ul><li>if  (listA == listB)... </li></ul><ul><ul><li>- The expression  listA == listB  de...
Relational Operators and Arrays (continued) boolean   areEqualArrays ( int [] firstArray,  int [] secondArray) { if  (firs...
Arrays as Parameter Methods
Methods for Array Processing
Methods for Array Processing (continued)
Methods for Array Processing (continued)
Methods for Array Processing (continued)
<ul><li>Suppose that you want to determine whether  27  is in the list;  </li></ul><ul><li>First you compare  27  with  li...
<ul><li>Search for  10 </li></ul><ul><li>Search starts at the first element in the list, that is, at  list[0] </li></ul><u...
<ul><li>public static int  seqSearch( int [] list,  </li></ul><ul><li>int  listLength,  int  searchItem) </li></ul><ul><li...
Arrays of Objects <ul><li>Can use arrays to manipulate objects </li></ul><ul><li>Example:  create array named  array1  wit...
Array of  String  Objects <ul><li>String[] nameList =  new  String[5];  </li></ul><ul><li>nameList[0] = &quot;Amanda Green...
Array of  String  Objects (continued)
Clock[] arrivalTimeEmp =  new  Clock[100];   Arrays of Objects (continued)
Instantiating Array Objects for  ( int  j = 0; j < arrivalTimeEmp.length; j++)  arrivalTimeEmp[j] =  new  Clock();
arrivalTimeEmp[49].setTime(8, 5, 10);   Instantiating Array Objects (continued)
Arrays and Variable Length Parameter List <ul><li>The syntax to declare a variable length formal parameter (list) is: </li...
Arrays and Variable Length Parameter List (continued)
Arrays and Variable Length Parameter List (continued)
Arrays and Variable Length Parameter List (continued) <ul><li>A method can have both a variable length formal parameter an...
Arrays and Variable Length Parameter List (continued) <ul><li>A method can have at most one variable length formal paramet...
foreach  Loop  <ul><li>The syntax to use this  for  loop to process the elements of an array is: </li></ul><ul><li>for  (d...
foreach  loop (continued) <ul><li>sum = 0;  </li></ul><ul><li>for  ( double  num : list)  </li></ul><ul><li>sum = sum + nu...
Two-Dimensional Arrays
Two-Dimensional Arrays (continued)
double [][] sales =  new   double [10][5]; Two-Dimensional Arrays (continued)
<ul><li>intExp1, intExp2 >= 0 </li></ul><ul><li>indexExp1  = row position </li></ul><ul><li>indexExp2  = column position <...
Accessing Array Elements (continued)
<ul><li>This statement declares and instantiates a two-dimensional array matrix of 20 rows and 15 columns </li></ul><ul><l...
Two-Dimensional Arrays and the Instance Variable  length  (continued)  <ul><li>Each row of matrix is a one-dimensional arr...
Two-Dimensional Arrays: Special Cases
Two-Dimensional Arrays: Special Cases (continued) <ul><li>Create columns </li></ul>
Two-Dimensional Array Initialization During Declaration
Two-Dimensional Array Initialization During Declaration (continued) <ul><li>To initialize a two-dimensional array when it ...
Two-Dimensional Array Initialization During Declaration (continued)
Two-Dimensional Arrays (continued) <ul><li>Three ways to process 2-D arrays </li></ul><ul><ul><li>Entire array </li></ul><...
Two-Dimensional Arrays: Processing Initialization for  ( int  row = 0; row < matrix.length; row++) for  ( int   col = 0; c...
Two-Dimensional Arrays: Processing (continued) Input for  ( int   row = 0; row < matrix.length; row++) for  ( int   col = ...
Two-Dimensional Arrays: Processing (continued) Sum by Column   for  ( int   col = 0; col < matrix[0].length; col++) { sum ...
Two-Dimensional Arrays: Processing (continued) Largest Element in Each Row for  ( int   row = 0; row < matrix.length; row+...
Two-Dimensional Arrays: Processing (continued) Largest Element in Each Column for  ( int   col = 0; col < matrix[0].length...
 
 
 
Multidimensional Arrays <ul><li>Can define three-dimensional arrays or n-dimensional array (n can be any number) </li></ul...
Loops to Process Multidimensional Arrays double [][][] carDealers =  new double [10][5][7]; for  ( int   i = 0; i < 10; i+...
Programming Example:  Text Processing <ul><li>Program:  reads given text; outputs the text as is; prints number of lines a...
Programming Example Solution:  Text Processing <ul><li>An array of 26 representing the letters in the alphabet </li></ul><...
Chapter Summary <ul><li>Arrays </li></ul><ul><ul><li>Definition </li></ul></ul><ul><ul><li>Uses </li></ul></ul><ul><li>Dif...
Chapter Summary (continued) <ul><li>Declaring arrays </li></ul><ul><li>Instantiating arrays </li></ul><ul><li>Processing a...
Upcoming SlideShare
Loading in...5
×

9781439035665 ppt ch09

942

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
942
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
41
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

9781439035665 ppt ch09

  1. 1. Java Programming: From Problem Analysis to Program Design, 4e Chapter 9 Arrays
  2. 2. Chapter Objectives <ul><li>Learn about arrays </li></ul><ul><li>Explore how to declare and manipulate data into arrays </li></ul><ul><li>Understand the meaning of “array index out of bounds” </li></ul><ul><li>Become familiar with the restrictions on array processing </li></ul>
  3. 3. Chapter Objectives (continued) <ul><li>Discover how to pass an array as a parameter to a method </li></ul><ul><li>Discover how to manipulate data in a two-dimensional array </li></ul><ul><li>Learn about multidimensional arrays </li></ul>
  4. 4. Array <ul><li>Definition: structured data type with a fixed number of elements </li></ul><ul><li>Elements of an array are also called components of the array </li></ul><ul><li>Every element is of the same type </li></ul><ul><li>Elements are accessed using their relative positions in the array </li></ul>
  5. 5. One-Dimensional Arrays
  6. 6. One-Dimensional Arrays (continued)
  7. 7. One-Dimensional Arrays (continued) <ul><li>intExp = number of components in array >= 0 </li></ul><ul><li>0 <= indexExp <= intExp </li></ul>
  8. 8. <ul><li>Array num: int[] num = new int[5]; </li></ul>Arrays
  9. 9. <ul><li>Array num: int[] num = new int[5]; </li></ul>Arrays (continued)
  10. 10. Array List
  11. 11. Array List (continued)
  12. 12. Array List (continued)
  13. 13. Array List (continued)
  14. 14. <ul><li>Specifying Array Size During Program Execution </li></ul>
  15. 15. <ul><li>The initializer list contains values, called initial values , that are placed between braces and separated by commas </li></ul><ul><li>sales[0]= 12.25 , sales[1]= 32.50 , sales[2]= 16.90 , sales[3]= 23.00 , and sales[4]= 45.68 </li></ul>Array Initialization During Declaration
  16. 16. Array Initialization During Declaration (continued) <ul><li>When declaring and initializing arrays, the size of the array is determined by the number of initial values within the braces </li></ul><ul><li>If an array is declared and initialized simultaneously, we do not use the operator new to instantiate the array object </li></ul>
  17. 17. <ul><li>Associated with each array that has been instantiated, there is a public ( final ) instance variable length </li></ul><ul><li>The variable length contains the size of the array </li></ul><ul><li>The variable length can be directly accessed in a program using the array name and the dot operator </li></ul><ul><li>int [] list = {10, 20, 30, 40, 50, 60}; </li></ul>Arrays and the Instance Variable length
  18. 18. Arrays and the Instance Variable length (continued) <ul><li>This statement creates the array list of six components and initializes the components using the values given </li></ul><ul><ul><li>Here list.length is 6 </li></ul></ul><ul><li>int [] numList = new int[10]; </li></ul><ul><li>This statement creates the array numList of 10 components and initializes each component to 0 </li></ul>
  19. 19. <ul><li>The value of numList.length is 10 </li></ul><ul><li>numList[0] = 5; </li></ul><ul><li>numList[1] = 10; </li></ul><ul><li>numList[2] = 15; </li></ul><ul><li>numList[3] = 20; </li></ul><ul><li>These statements store 5 , 10 , 15 , and 20 , respectively, in the first four components of numList </li></ul><ul><li>You can store the number of filled elements, that is, the actual number of elements, in the array in a variable, say numOfElement </li></ul><ul><li>It is a common practice for a program to keep track of the number of filled elements in an array </li></ul>Arrays and the Instance Variable length (continued)
  20. 20. <ul><li>Loops used to step through elements in array and perform operations </li></ul><ul><li>int [] list = new int[100]; </li></ul><ul><li>int i; </li></ul><ul><li>for (i = 0; i < list.length; i++) </li></ul><ul><li>//process list[i], the (i + 1)th </li></ul><ul><li>//element of list </li></ul><ul><li>for (i = 0; i < list.length; i++) </li></ul><ul><li>list[i] = console.nextInt(); </li></ul><ul><li>for (i = 0; i < list.length; i++) </li></ul><ul><li>System.out.print(list[i] + &quot; &quot;); </li></ul>Processing One-Dimensional Arrays
  21. 21. Arrays (continued) <ul><li>Some operations on arrays </li></ul><ul><ul><li>Initialize </li></ul></ul><ul><ul><li>Input data </li></ul></ul><ul><ul><li>Output stored data </li></ul></ul><ul><ul><li>Find largest/smallest/sum/average of elements </li></ul></ul>double [] sales = new double [10]; int index; double largestSale, sum, average;
  22. 22. Code to Initialize Array to Specific Value (10.00) for ( int index = 0; index < sales.length; index++) sales[index] = 10.00;
  23. 23. Code to Read Data into Array for ( int index = 0; index < sales.length; index++) sales[index] = console.nextDouble();
  24. 24. Code to Print Array for ( int index = 0; index < sales.length; index++) System.out.print(sales[index] + &quot; &quot;);
  25. 25. Code to Find Sum and Average of Array sum = 0; for ( int index = 0; index < sales.length; index++) sum = sum + sales[index]; if (sales.length != 0) average = sum / sales.length; else average = 0.0;
  26. 26. Determining Largest Element in Array maxIndex = 0; for ( int index = 1; index < sales.length; index++) if (sales[maxIndex] < sales[index]) maxIndex = index; largestSale = sales[maxIndex];
  27. 27. Determining Largest Element in Array (continued)
  28. 28. Determining Largest Element in Array (continued)
  29. 29. Array Index Out of Bounds <ul><li>Array in bounds if: </li></ul><ul><li>0 <= index <= arraySize – 1 </li></ul><ul><li>If index < 0 or index > arraySize : </li></ul><ul><li>ArrayIndexOutOfBoundsException exception is thrown </li></ul><ul><li>Base address: memory location of first component in array </li></ul>
  30. 30. Declaring Arrays as Formal Parameters to Methods <ul><li>A general syntax to declare an array as a formal parameter </li></ul><ul><li>dataType[] arrayName </li></ul><ul><li>public static void arraysAsFormalParameter( int [] listA, </li></ul><ul><li>double [] listB, int num) </li></ul><ul><li>{ </li></ul><ul><li>//... </li></ul><ul><li>} </li></ul><ul><li>int [] intList = new int [10]; </li></ul><ul><li>double [] doubleNumList = new double [15]; </li></ul><ul><li>int number; </li></ul><ul><li>arraysAsFormalParameter(intList, doubleNumList, number); </li></ul>
  31. 31. The Assignment Operators and Arrays
  32. 32. The Assignment Operators and Arrays (continued)
  33. 33. The Assignment Operators and Arrays (continued)
  34. 34. Relational Operators and Arrays <ul><li>if (listA == listB)... </li></ul><ul><ul><li>- The expression listA == listB determines if the values of listA and listB are the same and thus determines whether listA and listB refer to the same array </li></ul></ul><ul><ul><li>- To determine whether listA and listB contain the same elements, you need to compare them component by component </li></ul></ul><ul><ul><li>- You can write a method that returns true if two int arrays contain the same elements </li></ul></ul>
  35. 35. Relational Operators and Arrays (continued) boolean areEqualArrays ( int [] firstArray, int [] secondArray) { if (firstArray.length != secondArray.length) return false ; for ( int index = 0; index < firstArray.length; index++) if (firstArray[index] != secondArray[index]) return false ; return true ; } if (areEqualArrays(listA, listB)) ...
  36. 36. Arrays as Parameter Methods
  37. 37. Methods for Array Processing
  38. 38. Methods for Array Processing (continued)
  39. 39. Methods for Array Processing (continued)
  40. 40. Methods for Array Processing (continued)
  41. 41. <ul><li>Suppose that you want to determine whether 27 is in the list; </li></ul><ul><li>First you compare 27 with list[0] </li></ul><ul><li>Because list[0] ≠ 27 , you then compare 27 with list[1] </li></ul><ul><li>Because list[1] ≠ 27 , you compare 27 with list[2] ; Because list[2] = 27 , the search stops </li></ul><ul><li>This search is successful </li></ul>
  42. 42. <ul><li>Search for 10 </li></ul><ul><li>Search starts at the first element in the list, that is, at list[0] </li></ul><ul><li>This time, the search item, which is 10 , is compared with every item in the list; eventually, no more data is left in the list to compare with the search item; this is an unsuccessful search </li></ul>
  43. 43. <ul><li>public static int seqSearch( int [] list, </li></ul><ul><li>int listLength, int searchItem) </li></ul><ul><li>{ </li></ul><ul><li>int loc; </li></ul><ul><li>boolean found = false ; </li></ul><ul><li>loc = 0; </li></ul><ul><li>while (loc < listLength && !found) </li></ul><ul><li>if (list[loc] == searchItem) </li></ul><ul><li>found = true ; </li></ul><ul><li>else </li></ul><ul><li>loc++; </li></ul><ul><li>if (found) </li></ul><ul><li>return loc; </li></ul><ul><li>else </li></ul><ul><li>return -1; </li></ul><ul><li>} </li></ul>
  44. 44. Arrays of Objects <ul><li>Can use arrays to manipulate objects </li></ul><ul><li>Example: create array named array1 with N objects of type T </li></ul><ul><li>T[] array1 = new T[N] </li></ul><ul><li>Can instantiate array1 as follows: </li></ul><ul><li>for ( int j = 0; j <array1.length; j++) </li></ul><ul><li> array1[j] = new T(); </li></ul>
  45. 45. Array of String Objects <ul><li>String[] nameList = new String[5]; </li></ul><ul><li>nameList[0] = &quot;Amanda Green&quot;; </li></ul><ul><li>nameList[1] = &quot;Vijay Arora&quot;; </li></ul><ul><li>nameList[2] = &quot;Sheila Mann&quot;; </li></ul><ul><li>nameList[3] = &quot;Rohit Sharma&quot;; </li></ul><ul><li>nameList[4] = &quot;Mandy Johnson&quot;; </li></ul>
  46. 46. Array of String Objects (continued)
  47. 47. Clock[] arrivalTimeEmp = new Clock[100]; Arrays of Objects (continued)
  48. 48. Instantiating Array Objects for ( int j = 0; j < arrivalTimeEmp.length; j++) arrivalTimeEmp[j] = new Clock();
  49. 49. arrivalTimeEmp[49].setTime(8, 5, 10); Instantiating Array Objects (continued)
  50. 50. Arrays and Variable Length Parameter List <ul><li>The syntax to declare a variable length formal parameter (list) is: </li></ul><ul><li>dataType ... identifier </li></ul>
  51. 51. Arrays and Variable Length Parameter List (continued)
  52. 52. Arrays and Variable Length Parameter List (continued)
  53. 53. Arrays and Variable Length Parameter List (continued) <ul><li>A method can have both a variable length formal parameter and other formal parameters; consider the following method heading: </li></ul><ul><li>public static void myMethod(String name, </li></ul><ul><li>double num, int ... intList) </li></ul><ul><li>The formal parameter name is of type String , the formal parameter num is of type double , and the formal parameter intList is of variable length </li></ul><ul><li>The actual parameter corresponding to intList can be an int array or any number of int variables and/or int values </li></ul>
  54. 54. Arrays and Variable Length Parameter List (continued) <ul><li>A method can have at most one variable length formal parameter </li></ul><ul><li>If a method has both a variable length formal parameter and other types of formal parameters, then the variable length formal parameter must be the last formal parameter of the formal parameter list </li></ul>
  55. 55. foreach Loop <ul><li>The syntax to use this for loop to process the elements of an array is: </li></ul><ul><li>for (dataType identifier : arrayName) </li></ul><ul><li>statements </li></ul><ul><li>identifier is a variable, and the data type of identifier is the same as the data type of the array components </li></ul>
  56. 56. foreach loop (continued) <ul><li>sum = 0; </li></ul><ul><li>for ( double num : list) </li></ul><ul><li>sum = sum + num; </li></ul><ul><li>The for statement in Line 2 is read: for each num in list </li></ul><ul><li>The identifier num is initialized to list[0] </li></ul><ul><li>In the next iteration, the value of num is list[1] , and so on </li></ul><ul><li>for ( double num : numList) </li></ul><ul><li>{ </li></ul><ul><li>if (max < num) </li></ul><ul><li>max = num; </li></ul><ul><li>} </li></ul>
  57. 57. Two-Dimensional Arrays
  58. 58. Two-Dimensional Arrays (continued)
  59. 59. double [][] sales = new double [10][5]; Two-Dimensional Arrays (continued)
  60. 60. <ul><li>intExp1, intExp2 >= 0 </li></ul><ul><li>indexExp1 = row position </li></ul><ul><li>indexExp2 = column position </li></ul>Accessing Array Elements
  61. 61. Accessing Array Elements (continued)
  62. 62. <ul><li>This statement declares and instantiates a two-dimensional array matrix of 20 rows and 15 columns </li></ul><ul><li>The value of the expression: </li></ul><ul><li>matrix.length </li></ul><ul><li>is 20, the number of rows </li></ul>Two-Dimensional Arrays and the Instance Variable length
  63. 63. Two-Dimensional Arrays and the Instance Variable length (continued) <ul><li>Each row of matrix is a one-dimensional array; matrix[0] , in fact, refers to the first row </li></ul><ul><li>The value of the expression: </li></ul><ul><li>matrix[0].length </li></ul><ul><li>is 15, the number of columns in the first row </li></ul><ul><li>matrix[1].length gives the number of columns in the second row, which in this case is 15, and so on </li></ul>
  64. 64. Two-Dimensional Arrays: Special Cases
  65. 65. Two-Dimensional Arrays: Special Cases (continued) <ul><li>Create columns </li></ul>
  66. 66. Two-Dimensional Array Initialization During Declaration
  67. 67. Two-Dimensional Array Initialization During Declaration (continued) <ul><li>To initialize a two-dimensional array when it is declared: </li></ul><ul><ul><li>- The elements of each row are enclosed within braces and separated by commas </li></ul></ul><ul><ul><li>- All rows are enclosed within braces </li></ul></ul>
  68. 68. Two-Dimensional Array Initialization During Declaration (continued)
  69. 69. Two-Dimensional Arrays (continued) <ul><li>Three ways to process 2-D arrays </li></ul><ul><ul><li>Entire array </li></ul></ul><ul><ul><li>Particular row of array (row processing) </li></ul></ul><ul><ul><li>Particular column of array (column processing) </li></ul></ul><ul><li>Processing algorithms similar to processing algorithms of one-dimensional arrays </li></ul>
  70. 70. Two-Dimensional Arrays: Processing Initialization for ( int row = 0; row < matrix.length; row++) for ( int col = 0; col < matrix[row].length; col++) matrix[row][col] = 10; Print for ( int row = 0; row < matrix.length; row++) { for ( int col = 0; col < matrix[row].length; col++) System.out.printf(&quot;%7d&quot;, matrix[row][col]); System.out.println(); }
  71. 71. Two-Dimensional Arrays: Processing (continued) Input for ( int row = 0; row < matrix.length; row++) for ( int col = 0; col < matrix[row].length; col++) matrix[row][col] = console.nextInt(); Sum by Row for ( int row = 0; row < matrix.length; row++) { sum = 0; for ( int col = 0; col < matrix[row].length; col++) sum = sum + matrix[row][col]; System.out.println(&quot;Sum of row &quot; + (row + 1) + &quot; = &quot;+ sum); }
  72. 72. Two-Dimensional Arrays: Processing (continued) Sum by Column for ( int col = 0; col < matrix[0].length; col++) { sum = 0; for ( int row = 0; row < matrix.length; row++) sum = sum + matrix[row][col]; System.out.println(&quot;Sum of column &quot; + (col + 1) + &quot; = &quot; + sum); }
  73. 73. Two-Dimensional Arrays: Processing (continued) Largest Element in Each Row for ( int row = 0; row < matrix.length; row++) { largest = matrix[row][0]; for ( int col = 1; col < matrix[row].length; col++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println(&quot;The largest element of row &quot; + (row + 1) + &quot; = &quot; + largest); }
  74. 74. Two-Dimensional Arrays: Processing (continued) Largest Element in Each Column for ( int col = 0; col < matrix[0].length; col++) { largest = matrix[0][col]; for ( int row = 1; row < matrix.length; row++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println(&quot;The largest element of col &quot; + (col + 1) + &quot; = &quot; + largest); }
  75. 78. Multidimensional Arrays <ul><li>Can define three-dimensional arrays or n-dimensional array (n can be any number) </li></ul><ul><li>Syntax to declare and instantiate array </li></ul><ul><li>d ataType[][]…[] arrayName = new </li></ul><ul><li>dataType[intExp1][intExp2]…[intExpn]; </li></ul><ul><li>Syntax to access component </li></ul><ul><li>arrayName[indexExp1][indexExp2]…[indexExpn] </li></ul><ul><ul><ul><li>intExp1 , intExp2 , ..., intExpn = positive integers </li></ul></ul></ul><ul><ul><ul><li>indexExp1,indexExp2 , ..., indexExpn = non-negative integers </li></ul></ul></ul>
  76. 79. Loops to Process Multidimensional Arrays double [][][] carDealers = new double [10][5][7]; for ( int i = 0; i < 10; i++) for ( int j = 0; j < 5; j++) for ( int k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;
  77. 80. Programming Example: Text Processing <ul><li>Program: reads given text; outputs the text as is; prints number of lines and number of times each letter appears in text </li></ul><ul><li>Input: file containing text to be processed </li></ul><ul><li>Output: file containing text, number of lines, number of times letter appears in text </li></ul>
  78. 81. Programming Example Solution: Text Processing <ul><li>An array of 26 representing the letters in the alphabet </li></ul><ul><li>Three methods </li></ul><ul><ul><li>copyText </li></ul></ul><ul><ul><li>characterCount </li></ul></ul><ul><ul><li>writeTotal </li></ul></ul><ul><li>Value in appropriate index incremented using methods and depending on character read from text </li></ul>
  79. 82. Chapter Summary <ul><li>Arrays </li></ul><ul><ul><li>Definition </li></ul></ul><ul><ul><li>Uses </li></ul></ul><ul><li>Different Arrays </li></ul><ul><ul><li>One-dimensional </li></ul></ul><ul><ul><li>Two-dimensional </li></ul></ul><ul><ul><li>Multidimensional (n-dimensional) </li></ul></ul><ul><ul><li>Arrays of objects </li></ul></ul><ul><ul><li>Parallel arrays </li></ul></ul>
  80. 83. Chapter Summary (continued) <ul><li>Declaring arrays </li></ul><ul><li>Instantiating arrays </li></ul><ul><li>Processing arrays </li></ul><ul><ul><li>Entire array </li></ul></ul><ul><ul><li>Row processing </li></ul></ul><ul><ul><li>Column processing </li></ul></ul><ul><li>Common operations and methods performed on arrays </li></ul><ul><li>Manipulating data in arrays </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×