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

9781111530532 ppt ch09

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