Arrays
Arrays
Arrays
• A single array variable can reference a large
collection of data.
• Once an array is created, its size is fixed.
• An array reference variable is used to access the
elements in an array using an index.
Declaring an Array
• To use an array in a program, you must declare a variable
to reference the array and specify the array’s element
type. Here is the syntax for declaring an array variable:
elementType[] arrayRefVar;
• The elementType can be any data type, and all elements
in the array will have the same data type. For example,
the following code declares a variable myList that
references an array of double elements.
double[] myList;
• You can also use elementType arrayRefVar[] to declare
an array variable. The style elementType[] arrayRefVar is
preferred.
Creating Arrays
• Syntax: arrayRefVar = new datatype[arraySize];
• Example:
• myList = new double[10];
• myList[0] references the first element in the array.
• myList[9] references the last element in the array.
• We can combine into one statement:
• Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Passing Arrays to Methods
• When passing an array to a method, the reference of
the array is passed to the method.
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + " ");
}}
• Invoke the method: int[] list = {3, 1, 2, 6, 4, 2};
printArray(list);
Or printArray(new int[]{3, 1, 2, 6, 4, 2});
Anonymous Array
Note
• Java uses pass-by-value to pass arguments to a method.
There are important differences between passing the values
of variables of primitive data types and passing arrays.
• For an argument of a primitive type, the argument’s value is
passed.
• For an argument of an array type, the value of the argument
is a reference to an array; this reference value is passed to the
method. Semantically, it can be best described as
pass-by-sharing, that is, the array in the method is the same
as the array being passed.
• Thus, if you change the array in the method, you will see the
change outside the method.
Foreach loop
• Java supports a convenient for loop, known as a foreach loop. It
Enables array traversal sequentially without using an index
variable
• Syntax:
for (elementType element: arrayRefVar) {
// Process the element
}
• “for each element e in myList, do the following.”
for (double e: myList) {
System.out.println(e);
}
– Variable, e, must be declared as the same type as the
elements in myList.
– You still have to use an index variable if you wish to
traverse the array in a different order or change the
elements in the array.
Returning array from method
• When a method returns an array, the reference of the array is returned
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
Declaring, Creating, and Initializing Using
Shorthand Notations
You can also use an array initializer to declare, create and
initialize a two-dimensional array.
For example,
int[][] array = {
{1, 2, 3}, int[][] array = new int[4][3];
{4, 5, 6}, array[0][0] = 1; array[0][1] = 2;
Same as array[0][2] = 3;
{7, 8, 9},
array[1][0] = 4; array[1][1] = 5;
{10, 11, 12}
array[1][2] = 6;
};
array[2][0] = 7; array[2][1] = 8;
array[2][2] = 9;
array[3][0] = 10;array[3][1] = 11;
array[3][2] = 12;
Lengths of Two-dimensional Arrays
array[4].length ArrayIndexOutOfBoundsException
Ragged Arrays
Each row in a two-dimensional array is itself an array.
So, the rows can have different lengths.
Such an array is known as a ragged array.
For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
matrix.length is 5
{2, 3, 4, 5},
{3, 4, 5}, matrix[0].length is 5
matrix[1].length is 4
{4, 5}, matrix[2].length is 3
{5} matrix[3].length is 2
}; matrix[4].length is 1
Ragged Arrays, cont.
• If we don’t know the values in a ragged array in
advance, but do know the sizes—we can create a
ragged array using the following syntax:
• int[][] triangleArray = new int[5][];
• triangleArray[0] = new int[5];
• triangleArray[1] = new int[4];
• triangleArray[2] = new int[3];
• triangleArray[3] = new int[2];
• triangleArray[4] = new int[1];
• we can now assign values to the array. For example,
• triangleArray[0][3] = 50;
• triangleArray[4][0] = 45;
The ArrayList Class
• we can create an array to store objects. But the array’s size is
fixed once the array is created. Java provides the ArrayList class
that can be used to store an unlimited number of objects.
• ArrayList is known as a generic class with a generic type E.
You can specify a concrete type to replace E when creating
an ArrayList. For example, the following statement
creates an ArrayList and assigns its reference to variable
cities. This ArrayList object can be used to store strings.
ArrayList<String> cities = new ArrayList<String>();
• The following statement creates an ArrayList and assigns
its reference to variable dates. This ArrayList object can
be used to store dates.
ArrayList<java.util.Date> dates = new ArrayList<java.util.Date> ();
ArrayList<AConcreteType> list = new ArrayList<AConcreteType>();
can be simplified by
ArrayList<AConcreteType> list = new ArrayList<>();
Important Fact
• A generic class is shared by all its instances regardless of its actual
concrete type.
• Suppose list1 and list2 are created as follows:
ArrayList<String> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
• Although ArrayList<String> and ArrayList<Integer> are two types at
compile time, only one ArrayList class is loaded into the JVM at
runtime.
• list1 and list2 are both instances of ArrayList, so the following
statements display true:
System.out.println(list1 instanceof ArrayList);
System.out.println(list2 instanceof ArrayList);
• However, list1 instanceof ArrayList<String> is wrong
• Since ArrayList<String> is not stored as a separate class in the JVM,
using it at runtime makes no sense.
Differences and Similarities between Arrays and
ArrayList
ArrayList …
• Suppose you want to create an ArrayList for storing
integers. Can you use the following code to create a list?
ArrayList<int> listOfIntegers = new ArrayList<>();
• No.
• This will not work because the elements stored in an
ArrayList must be of an object type.
• You cannot use a primitive data type such as int to
replace a generic type.
• However, you can create an ArrayList for storing
Integer objects as follows: