Vector
class. In this section you will
learn the basics of arrays in Java.
elementType[] arrayName;
elementType arrayName[];
We defined an array of integers and of strings, respectively.int[] arrayOfInts; String arrayOfStrings[];
new
operator.
The following statement instantiates the
arrayOfInts
declared above to
contain five integer elements.
We say that the size of the array (or the length of the array) equals five.arrayOfInts = new int[5];
Both Java commands could have been combined into one statement:
In general, you will often see statements of the formint[] arrayOfInts = new int[5];
elementType[] arrayName = new elementType[arraySize]
creates an array of four strings. However, you cannot do this as an argument within a method. Then you separate the instantiation and the argument use.String[] arrayOfStrings = {"Joyful", "And", "Valuable", "Applets"};
length
instance variable.
Thus, the following array of integers has five elements accessible via
p[0]
, p[1]
, p[2]
, p[3]
,
and p[4]
, respectively.
It is in most cases unwieldy to specify each element individually. It is often helpful to useint[] p = new int[5]; p[0] = 2; p[1] = 3; p[2] = 5; p[3] = 7; p[4] = 11; p.length; // array size is 5
for
loops to initialize an array.
The next example shows how you can build-up a table of the first fifty Fibonacci numbers. Recall the definition of Fibonacci numbers Fn by F0 = F1 = 1 and Fn = Fn-1 + Fn-2 for n>1.int[] squares = new int[50]; for (int i=0; i < squares.length; i++) { squares[i] = i*i; }
public class Fibonacci { public static void main(String[] args) { long fib[] = new long[50]; fib[0] = 1; fib[1] = 1; for (int i=2; i<fib.length; i++) { fib[i] = fib[i-1] + fib[i-2]; } for (int i=0; i<fib.length; i++) { System.out.println("fib[" + i + "] = " + fib[i]); } } }
fib[0] = 1 fib[1] = 1 fib[2] = 2 fib[3] = 3 fib[4] = 5 fib[5] = 8 fib[6] = 13 fib[7] = 21 fib[8] = 34 fib[9] = 55 ... ... fib[44] = 1134903170 fib[45] = 1836311903 fib[46] = 2971215073 fib[47] = 4807526976 fib[48] = 7778742049 fib[49] = 12586269025
ArrayIndexOutOfBoundsException
is thrown.
System
class provides a method called arraycopy
for copying a range of elements from one array to another. It can be
used to make a copy of a whole array, which can be manipulated without
changing the original array. The assignment of one array to another via
the =
assignment operator would destroy elements in the
original array while changing elements in the copy. The following example
clarifies this.
public class copyTest { public static void main(String[] args) { // initialization of arrays int[] a = {1,2,3}; int[] b = a; int[] c = new int[3]; System.arraycopy(a,0,c,0,3); // printing arrays for (int i=0; i<3; i++) { System.out.println("a["+i+"] = "+a[i]+ ", b["+i+"] = "+b[i]+", c["+i+"] = "+c[i]); } System.out.println(""); // changing c for (int i=0; i<3; i++) { c[i]++; } // printing arrays for (int i=0; i<3; i++) { System.out.println("a["+i+"] = "+a[i]+ ", b["+i+"] = "+b[i]+", c["+i+"] = "+c[i]); } System.out.println("c changed; a and b unchanged"); // changing b for (int i=0; i<3; i++) { b[i]--; } // printing arrays for (int i=0; i<3; i++) { System.out.println("a["+i+"] = "+a[i]+ ", b["+i+"] = "+b[i]+", c["+i+"] = "+c[i]); } System.out.println("c unchanged; a and b changed"); } }
a[0] = 1, b[0] = 1, c[0] = 1 a[1] = 2, b[1] = 2, c[1] = 2 a[2] = 3, b[2] = 3, c[2] = 3 a[0] = 1, b[0] = 1, c[0] = 2 a[1] = 2, b[1] = 2, c[1] = 3 a[2] = 3, b[2] = 3, c[2] = 4 c changed; a and b unchanged a[0] = 0, b[0] = 0, c[0] = 2 a[1] = 1, b[1] = 1, c[1] = 3 a[2] = 2, b[2] = 2, c[2] = 4 c unchanged; a and b changed
// a 3x3-matrix of integers (2-dim. array) // int[][] matrix = new int[3][3]; for (int row=0; row<3; row++) { for (int col=0; col<3; col++) { matrix[row][col] = row + col; } } // // an array of size 3 consisting of 2-dim. arrays // int[][][] arrayOfMatrices = new int[3][][]; int[][] a = {{1,2}, {3,4}}; int[][] b = {{1,2,3}, {4,5,6}}; int[][] c = {{1,2}, {3,4}, {5,6}}; arrayOfMatrices[0] = a; arrayOfMatrices[1] = b; arrayOfMatrices[2] = c; // // a non-rectangular 2-dim. array // int[][] twodim = {{1}, {1,2}, {1,2,3}};