ARRAYS
To declare an array of integers:
int[ ] scores;
Initializer Lists
An initializer list can be used to instantiate and fill an array in one step. The values are delimited by braces and separated by commas: • Examples int[] units = {147, 323, 89, 933, 540, 97, 114, 298, 476}; double[] numbers = {3.45, 6.54, -6.54}; char[] letterGrades = {'A', 'B', 'C', 'D', 'F'}; String[] days = {"Monday", "Tuesday", "Sunday"};
To declare an array
Arrays are objects, so we need to instantiate it with the reserved word "new" before we start using it: int[ ] scores = new int[10]; Here the size of the array is set to be 10. Now we can store 10 integers in this array.
Bounds Checking
The Java interpreter throws an "ArrayIndexOutOfBoundsException" if an array index is out of bounds. scores.length • Note that length holds the number of elements, not the largest index
The following loop collects inputs and fills up the values array:
int currentSize = 0; Scanner in = new Scanner(System.in); while (in.hasNextDouble()) { if (currentSize < values.length) { values[currentSize] = in.nextDouble(); currentSize++; } }
Here we search for the position of the first element in an array that is equal to 100:
int searchedValue = 100; int pos = 0; boolean found = false; while (pos < values.length && !found) { if (values[pos] == searchedValue) { found = true; } else { pos++; } } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); }
An array
is an ordered list of values - The entire array has a single name. - Each value has a numeric index - An array of size N is indexed from zero to N-1
Two-Dimensional Arrays
• A one-dimensional array stores a list of elements • A two-dimensional array can be thought of as a table of elements, with rows and columns
the two-dimensional array is an array of arraysT
• A two-dimensional array is declared by specifying the size of each dimension separately int[][] scores = new int[12][50]; • An array element is referenced using two index values value = scores[3][6] • The array stored in one row can be specified using one index
Arrays as Parameters
• An entire array can be passed as a parameter to a method • Like any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other • Therefore, changing an array element within the method changes the original • An individual array element can be passed to a method as well, in which case the type of the formal parameter is the same as the element type
Declaring Arrays
• By default, if you create an array of integers, it will have 0 value at each index. • If you create an array of doubles, it will have 0.0 value at each index. • If you create an array of chars, it will have a character ' ' (space) at each index. • If you create an array of booleans, it will have the false value at each index. • If you create an array of objects, it will have the null reference at each index (for each index, it needs to be instantiated before using it)
Using Arrays
• the for loop can be used when processing array elements for (int i=0; i<score.length; i++) System.out.println (score[i]);
This search process is called a binary search
boolean found = false; int low = 0; int high = values.length - 1; int pos = 0; while (low <= high && !found) { pos = (low + high) / 2; // Midpoint of the subsequence if (values[pos] == searchedNumber) { found = true; } else if (values[pos] < searchedNumber) { low = pos + 1; } // Look in second half else { high = pos - 1; } // Look in first half } if (found) { System.out.println("Found at position " + pos); } else { System.out.println("Not found. Insert before position " + pos); }
New Array
for (int i = 0; i < currentSize; i++) { System.out.println(values[i]); }
ReverseOrder.java
Scanner scan = new Scanner (System.in); double[] numbers = new double[10]; System.out.println ("The size of the array: " + numbers.length); for (int index = 0; index < numbers.length; index++) { System.out.print ("Enter number " + (index+1) + ": "); numbers[index] = scan.nextDouble(); } System.out.println ("The numbers in reverse order:"); for (int index = numbers.length-1; index >= 0; index--) System.out.print (numbers[index] + " ");
More about Arrays
The values held in an array are called array elements. • An array stores multiple values of the same type - the element type. • The element type can be a primitive type or an object reference.
