As you program, yo probably have come up with situations where you wish you could deal with a bunch of objects at a time; in Java, the most basic way of dealing with many objects at a time is by using arrays.
An array is a fixed-type collection of objects all of the same time. Array types are denoted by having [] after the base type (the type of all the objects in the array).Arrays have a property, length, which tells you the number of elements in the array, and a special operator,[] which gets you a given element from the array.
For example, we can declare an array variable and initialize it to an array of 3 elements like this (notice the [] on both sides):
int arr[] = new int[3];When declaring a variable only, we can create an array constant, enclosing the values in {} and separating with commas, as in:
int arr2[] = {1,2,3,4,5};Arrays have a property, length, which gives you the number of elements in the array.
We can access the elements in an array with the [] operator, passing it the index of the element we want. For example, if we want to print the first element of the array arr2 (which has index 0, arrays start at 0 in java), we would use:
System.out.println(arr2[0])We can also assign values to the elements of an array, with the same notation, so we could do something like this to set the first element to 50:
arr2[0]=50;We can pass arrays as parameters; as arrays are objects, they are passed by reference, so we can change the elements in the array.
A function that prints all elements in an array of ints, using a for loop to go through all the elements, would be:
public static void printArray(int[] arr){
for(int i=0; i<arr.length; ++i) {
System.out.println(arr[i]);
}
}And one that multiplies all elements of the array by 2 (showing you can change them) would be:
public static void multiplyBy2(int[] arr){
for(int i=0; i<arr.length; ++i) {
arr[i]=arr[i]*2;
}
}Most of our loops over arrays, end up looking like those above; using a variable for the index; given this, Java added a special loop to iterate through collections; we declare a variable for the element and we use a : to separate this variable from the array, as follows:
public static void printArrayForeach(int[] arr){
for(int elem: arr) {
System.out.println(elem);
}
} public static void multiplyBy2(int[] arr){
for(int elem: arr) {
elem=elem*2;
}
}Here are a couple of examples which do not quite have the typical loop:
The first one, is a function to calculate the minimum value in an array of ints;
public static int min(int[] arr) {
// we assume the array is not empty
int theMin=arr[0];
for(int i=1; i<arr.length; ++i) {
if(arr[i]<theMin)
theMin=arr[i];
}
return theMin;
} public static boolean isSorted(int[] arr) {
for(int i=1; i<arr.length; ++i) {
if(arr[i]>arr[i-1])
return false;
}
return true;
}The elements of an array may be objects of any complexity; in particular, they can also be other arrays !
For example we could have arrays of points:
Point[] points={ new Point(1,2), new Point(3,4)};or arrays of arrays of ints
int[][] arr={ {1,2}, {3,4}}- do something with all the elements
- do something to all the elements
- use an accumulator
- min / max
- sorting