Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CiS 260: App Dev I Introduction to Arrays An array is an object that contains a collection of components (_________) of the same data type. For a one-dimensional array, the collection is a simple ______. To declare a one-dimensional array int[] anArray; // reference variable declared int anArray []; // does the same thing To then __________ this one-dimensional array with 10 elements anArray = new int[ 10 ]; // elements declared You can do both in one statement int[] anArray = new int[ 10 ]; or int j = 10; int[] anArray = new int[ j ]; 2 Accessing Array Components An array component has the form anArray[ i ] where i ranges from __ to anArray.length - 1. [] is the array subscripting __________. anArray[ 0 ] is the first element in the array and can be treated like any other __________. anArray[ 0 ] = 5; assigns 5 to anArray[ 0 ]. The following statements also assign a value to an array element int i = 7; anArray[ i ] = 42; The following declares, instantiates, and loads double [] myArray = { 5.0, 10.0, 15.0, 20.0 };3 import java.util.Scanner; public class ReversePrintII { public static void main (String[] args ) { int[] item = new int[ 5 ]; int sum = 0, counter; Scanner input = new Scanner( System.in ); Example System.out.println( "Enter five integers, "one integer per line" ); for( counter = 0; counter < 5; counter++ ) { item[ counter ] = input.nextInt(); sum = sum + item[ counter ]; } // end for System.out.println( "The sum of the numbers = " + sum ); System.out.print( "The numbers in the reverse order: “ ); for( counter = 4; counter >= 0; counter-- ) System.out.print( item[counter] + " “ ); System.out.println(); } // end main } // end class Arrays as Parameters to Methods An array is an _______ and can be passed as an argument to a method. Example public static void main( String [] args ) { double [] myArray = { 5.0, 10.0, 15.0, 20.0 }; printArray( myArray ); } public static void printArray( double[] anArray ) { for( int i = 0; i < anArray.length; i++ ) System.out.println( anArray[ i ] ); } The _____ address of an array is the address of the first array component, such as myArray[0].5 Parallel Arrays Two or more arrays are parallel if their corresponding components (__________) hold related information. Example int [] studentID = new int[ 50 ]; double [] totalScore = new double[ 50 ]; char [] courseGrade = new char[ 50 ]; Thus, studentID[ 22 ], totalScore[ 22 ], and courseGrade[___] would all refer to the same student. 6 Arrays of Objects Arrays can hold __________ such as int’s, double’s and char’s. In Java, arrays can also hold _______ which in turn can hold data of differing types (such as primitives or other objects). Suppose you have employees and each employee has an arrival and a departure time. You can create arrays for employees, their arrival times, and their departure times: Employee[] companyEmp = new Employee[ 100 ]; Clock[] arrivalTimeEmp = new Clock[ 100 ]; Clock[] departureTimeEmp = new Clock[ 100 ]; 7 Two-Dimensional Arrays A one-dimensional array is used for data in list form (e.g., a list of names or a list of scores). A two-dimensional array is used for data in ____ form where all data have the same _____. Characteristics of tables are rows and _______. The general syntax is dataType[ ][ ] arrayName = new dataType[ rows] [ cols ]; Example of sales by month (0-11) for five car makers (Chrysler, GM, Ford, Honda, Toyota): double[][] sales = new double[ 12 ][ 5 ]; sales[ 5 ][ 3 ] = 25.75;//store in row 5, col 3 sales[ 7 ] is the sales array for all cars in _______ Ragged Two-Dimensional Arrays A ragged two-dim array has uneven _______. int [][] raggedArray = new int [ 3 ]; // # of rows raggedArray[0] = new int[ 4 ]; // # cols in 1st row raggedArray[1] = new int[ 1 ]; raggedArray[2] = new int[ 2 ]; raggedArray[0][0] = 6; raggedArray[0][1] = 17; … Load a two-dim array at declaration int [][] raggedArray = { {6,17,5,2},{14},{-2,8} }; Load a two-dim array using ________ loops for( int i = 0; i < raggedArray.length; i++ ) for( int j = 0; j < raggedArray[j].length; j++ ) raggedArray[ i ][ j ] = i; 9 Multidimensional Arrays Multi-dim arrays just extend two-dim arrays. Load random numbers in a 3-dim array double [][][] randomNums = new double [5][5][5]; for(int i=0; i<randomNums.length; i++) for(int j=0; j<randomNums[i][j].length; j++) for(int k=0; k<randomNums[i][j][k].length; k++) randomNums[i][j][k] = Math.random(); Add the random numbers in the array double sum = 0; for(int i = 0; i < randomNums.length; i++) for(int j = 0; j < randomNums[i][j].length; j++) for(int k = 0; k < randomNums[i][j][k].length; k++) sum += randomNums[i][j][k]; Predefined Methods for Arrays The class Arrays contains several useful methods: Arrays.toString( arrayA) // converts to Strings arrayA.equals( arrayB ) // tests “total” equality Arrays.sort( arrayA ) // sort arrayA ascending Arrays.search( arrayA, value ) // search arrayA Arrays.fill( arrayA, 0 ) // fill arrayA w/ 0’s 11