1 Arrays and Variable Length Parameter List The syntax to declare a variable length formal parameter (list) is: dataType... identifier
2 Arrays and Variable Length Parameter List public static double largest(double... numList) { double max; int index; if (numList.length != 0) { max = numList[0]; for (index = 1; index < numList.length; index++) { if (max < numList [index]) max = numList [index]; } return max; } return 0.0; }
3 Arrays and Variable Length Parameter List double num1 = largest(34, 56); double num2 = largest(12.56, 84, 92); double num3 = largest(98.32, 77, 64.67, 56); System.out.println(largest(22.50, 67.78, 92.58, 45, 34, 56)); double[] numberList = {18. 50, 44, 56.23, , 112.0, 77, 11, 22, 86.62); System.out.println(largest(numberList));
4 Rules to follow when using a variable length formal parameter list: 1.A method can have both a variable length formal parameter and other formal parameter. public static void myMethod (String name, double num, int … intList) 2.A method can have at most one varible length formal parameter. 3.If a method has both a variable length formal parameter and other formal parameter, then the variable length formal parameter must be the last). Arrays and Variable Length Parameter List
5 foreach loop The most recent version of Java provides a special type of for loop to process the elements of an objects such as an array. 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. This form of for is called a foreach loop.
6 foreach loop sum = 0; //line1 for (double num : list) //line2 sum = sum + num; //line3 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; }
7 [Red][Brown][Black][White][Gray] [GM] [Ford]63664 [Toyota]35367 [BMW]82753 [Nissan] [Volvo]15797 Two-Dimensional Arrays inStock
8 Two-Dimensional Arrays Data is sometimes in table form (difficult to represent using a one-dimensional array). To declare/instantiate a two-dimensional array: dataType[ ][ ] arrayName = new dataType[intExp1][intExp2]; To access a component of a two-dimensional array: arrayName[indexExp1][indexExp2]; intExp1, intExp2 >= 0 indexExp1 = row position indexExp2 = column position
9 double[][]sales = new double[10][5]; Two-Dimensional Arrays
10 Accessing Two-Dimensional Array Components =25.75
11 int [][] matrix = new int[20][15]; 20 rows 15 columns matrix.length = 20 //number of rows Each row of matrix is 1-D array matrix.[0].length = 15 //# of columns in 1 st row matrix.[1].length = 15 // # of columns in 2 nd row Two-Dimensional Arrays
12 Two-Dimensional Arrays: Special Cases Can specify different number of columns for each row (ragged arrays). In this case, each row must be instantiated separately.
13 Two-Dimensional Arrays: Special Cases To create this array, first we create 1-D array board of 5 rows: int[] board = new int[5]; board[0] = new int[6]; board[1] = new int[2]; board[2] = new int[2]; board[3] = new int[3]; board[4] = new int[4];
14 Two Dimensional Array Initialization During Declaration int[][] board = {{2,3,1}, {15,25,13}, {20, 4, 7}}; [0][1][2] [0]231 [1] [2]2047 board What is the array created after this statement: int[][] table = {{2,3,1,5}, {15,25}, {4, 23, 45}};
15 Processing Two Dimensional Array Three ways to process two-dimensional arrays: Entire array. Particular row of array (row processing). Particular column of array (column processing). Processing algorithms is similar to processing algorithms of one-dimensional arrays. The following declarations are used for our examples: static final int ROWS=7; static final int COLUMNS=6; int[][] matrix = new int [ROWS][COLUMNS]; int row, col, sum, largest, temp;
16 Loop for processing row 5 elements: for (col=0; col < matrix[5].length; col++) //process matrix[5][col] Loop for processing column 2 elements: for (row=0; row < matrix.length; row++) //process matrix[row][2] Two-Dimensional Arrays: Processing
17 Two-Dimensional Arrays: Processing Initialization for (row = 0; row < matrix.length; row++) for (col = 0; col < matrix[row].length; col++) matrix[row][col] = 10; Print for (row = 0; row < matrix.length; row++) { for (col = 0; col < matrix[row].length; col++) System.out.printf("%7d", matrix[row][col]); System.out.println(); }
18 Input for (row = 0; row < matrix.length; row++) for (col = 0; col < matrix[row].length; col++) matrix[row][col] = console.nextInt(); Sum by Row for (row = 0; row < matrix.length; row++) { sum = 0; for (col = 0; col < matrix[row].length; col++) sum = sum + matrix[row][col]; System.out.println("Sum of row " + (row + 1) + " = "+ sum); } Two-Dimensional Arrays: Processing
19 Sum by Column for (col = 0; col < matrix[0].length; col++) { sum = 0; for (row = 0; row < matrix.length; row++) sum = sum + matrix[row][col]; System.out.println("Sum of column " + (col + 1) + " = " + sum); } Two-Dimensional Arrays: Processing
20 Largest Element in Each Row for (row = 0; row < matrix.length; row++) { largest = matrix[row][0]; for (col = 1; col < matrix[row].length; col++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println("The largest element of row " + (row + 1) + " = " + largest); } Two-Dimensional Arrays: Processing
21 Largest Element in Each Column for (col = 0; col < matrix[0].length; col++) { largest = matrix[0][col]; for (row = 1; row < matrix.length; row++) if (largest < matrix[row][col]) largest = matrix[row][col]; System.out.println("The largest element of col " + (col + 1) + " = " + largest); } Two-Dimensional Arrays: Processing
22 Multidimensional Arrays Can define three-dimensional arrays or n-dimensional arrays (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
23 Loops to Process Multidimensional Arrays double[][][] carDealers = new double[10][5][7]; for (i = 0; i < 10; i++) for (j = 0; j < 5; j++) for (k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;
24 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 each letter appears in text.
25 Programming Example Solution: Text Processing An array of 26 representing the letters in the alphabet. Three methods: copyText characterCount writeTotal Value in appropriate index is incremented using methods and depends on character read from text.
26 Chapter Summary Arrays Definition Uses Different arrays One-dimensional Two-dimensional Multidimensional (n-dimensional) Arrays of objects Parallel arrays
27 Chapter Summary Declaring arrays Instantiating arrays Processing arrays Entire array Row processing Column processing Common operations and methods performed on arrays Manipulating data in arrays