9 - Lec - Arrays in Java Part-2
9 - Lec - Arrays in Java Part-2
• The brackets of the array type can be associated with the element type or with the
name of the array
• Therefore the following two declarations are equivalent:
float[] prices;
float prices[];
7-1
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:
7-2
Initializer Lists
• Note that when an initializer list is used:
– the new operator is not used
– no size value is specified
• The size of the array is determined by the number of items in the initializer list
• An initializer list can be used only in the array declaration
7-3
Primes
int[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19};
7-4
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
7-5
Arrays as Parameters
public static void doubleValues(int[] x) {
for (int i = 0; i < x.length; i++) {
x[i] *= 2;
}
}
7-6
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
one two
dimension dimensions
7-7
Two-Dimensional Arrays
• To be precise, in Java a two-dimensional array is an array of
arrays
• A two-dimensional array is declared by specifying the size of
each dimension separately:
Int rows = 5, cols = 10;
int[][] scores = new int[rows][cols];
• A array element is referenced using two index values:
value = scores[3][6]
• The array stored in one row can be specified using one index
7-8
Two-Dimensional Arrays
Expression Type Description
table[5][12] int[][] 2D array of integers, or
array of integer arrays
table[5] int[] array of integers
table int integer
7-9
Two Dimensional Array
public static void main (String[] args) {
int[][] table = new int[5][10];
7-11
Multidimensional Arrays
• An array can have many dimensions – if it has more
than one dimension, it is called a multidimensional array
• Each dimension subdivides the previous one into the
specified number of elements
• Each dimension has its own length constant
• Because each dimension is an array of array references,
the arrays within one dimension can be of different
lengths
– these are sometimes called ragged arrays
7-12
Ragged Arrays
• Ragged arrays have rows of unequal length
– each row has a different number of columns, or entries
• Example: create a 2-D int array named b with 5 elements in the first row, 7 in the
second row, and 4 in the third row:
int[][] b = new int[3][];
b[0] = new int[5];
b[1] = new int[7];
b[2] = new int[4];
Searching an Array
• There are many techniques for searching an array for a particular
value
• Sequential search:
– start at the beginning of the array and proceed in sequence until
either the value is found or the end of the array is reached*
• if the array is only partially filled, the search stops when the
last meaningful value has been checked
– it is not the most efficient way
– but it works and is easy to program
* Or, just as easy, start at the end and work backwards toward the
beginning
Example: Sequential Search of an Array
// return location of item if found; assume unique elements
// return -1 if item not found
public int searchArray (int[] array, int item)
{
int location = -1;
return location;
}
Using Arrays
• The iterator version of the for loop can be used when processing
array elements
for (int score : scores)
System.out.println (score);
• Usage
– Can access array elements
– Cannot modify array elements
– Cannot access the counter indicating the index
Array Implementations Overview
• Relatively fast insertions
• Iterating over the records simple
• Records are sorted by their insertion time
• If order needs to be preserved, removal is a difficult process,
otherwise deletions are fast, too.
• Finding an item needs sequential search, therefore not very efficient