MENU

Summary of basic array operations in Java

TOC

Initialize array upon declaration

The array can be initialized at the same time as the declaration by writing

sample code

static void arraySample() {
    String[] s = {"apple", "mandarin", "banana"}; }
}
System.out.println(s[1]);

Output Results:

unfinished


Get the number of elements in an array - -. length

If you want to know how many elements are in an array, use length, with the slight caveat that length is a field, not a method.

sample code

static void lengthSample() {
    String[] s1 = {"apple", "mandarin", "grape"};
    String[] s2 = {};
    System.out.println(s1.length);
    System.out.println(s2.length);
}

Output Results:

3
0

Copying arrays - clone

If you want to create an array that is identical to another array, clone is useful.

sample code

static void cloneSample() {
    String[] s1 = {"apple", "mandarin", "grape"};
    String[] s2 = s1.clone();
    System.out.println(s1[0] + ' ' + s1[1] + ' ' + s1[2]);
    System.out.println(s2[0] + ' ' + s2[1] + ' ' + s2[2]);
}

Output Results:

Apples Oranges Grapes
Apples, mandarins, grapes

Copying arrays - System.arraycopy

Arrays in Java are fixed at their initialization size. If you want to increase the size of an array once it has been created, you must use System.arraycopy.

arraycopy, like clone, is used to copy arrays, but it can be more finely tuned than clone.

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
 src... Array from which to copy
 srcPos... Starting position of the source array (src) to be copied
 dest... Destination array
 destPos... Starting position of the destination array (dest) to be copied
 length... Number of elements to copy

sample code

static void arraycopySample() {
    String[] s1 = {"apple", "mandarin", "grape"};
    String[] s2 = new String[5];
    System.arraycopy(s1, 0, s2, 0, 3);
    s2[3] = "banana";
    s2[4] = "cherry";
    System.out.println(s1[0] + " " + s1[1] + " " + s1[2]);
    System.out.println(s2[0] + " " + s2[1] + " " + s2[2] + " " + s2[3] + " " + s2[4]);
}

Output Results:

Apples Oranges Grapes
Apples Oranges Grapes Bananas Cherries

It is also convenient to use arraycopy to join arrays.

sample code

static void joinArraySample() {
    String[] s1 = {"apple", "mandarin", "grape"};
    String[] s2 = {"banana", "cherry", "momo"};
    int rLen = s1.length + s2.length;
    String[] rStr = new String[rLen]; //initialize array for length of both s1 and s2
    System.arraycopy(s1, 0, rStr, 0, s1.length);
    System.arraycopy(s2, 0, rStr, s1.length, s2.length); //Setting the starting position of the destination array is the key point
    System.out.println(rStr[0] + " " + rStr[1] + " " + rStr[2] + " " + rStr[3] + " " + rStr[4] + " " + rStr[5]);
}

Output Results:

Apples Oranges Grapes Grapes Bananas Cherries Momo

Create multidimensional arrays

You can create multidimensional arrays by creating arrays within arrays. This is useful for storing tabular data.

sample code

static void twoDArraySample() {
    int[] a1 = {38, 84, 98};
    int[] a2 = {32, 11, 56};
    int[] a3 = {82, 77, 8};
    int[][] twoDArray = {a1, a2, a3};
    for (int[] a : twoDArray) {
        for (int score : a) {
            System.out.print(score + " "); }
        }
        System.out.println(""); }
    }
}

Output Results:

38 84 98 
32 11 56 
82 77 8 
TOC