Java Tutorial: Arrays

, , …,

By appending a pair of square brackets after a datatype, you can have a array of that datatype. For example, in the declaration int[] v, it means that v is a array of type “int”.

A array in Java is like a fixed number of slots, each slot holds a item, and all of them the same type.

/* this example shows a array declaration, initialization, and
filling out the slots.
public class Arr {
    public static void main(String[] args) {

        // declare a array datatype, where in this case each slot
        // holds a int type.
        int[] myA;
        // note that code like "int[10] myA;" is illegal. Since java
        // expect a datatype declaration, and int[10] isn't a
        // datatype. The number of items are specified in other ways.

        // now java knows that myA is variable of datatype of array, of int.
        // You assign myA a value like this:
        myA = new int[10]; // 10 is the number of items

        // now myA is declared, and have assigned a value (initialized).
        // array assignment for each slot is like this:
        myA[3] = 6;
        System.out.print(myA[3] + "\n");

        // to get the length of a variable of array type, do:
        System.out.print(myA.length + "\n");

        // note that array index starts at 0 in Java. So,
        // myA[10] would give compilation error here.

        // if a slot has not given a value, it is garbage.
        // in this specific case, java fills it with 0.
        for (int i = 0; i < myA.length; i++) {
            System.out.print(myA[i] + " ");

        // try writing a program of array of a class.

Note the keyword “new” in myA = new int[10];, even though myA isn't a class. This is one of Java's syntax idiosyncrasy. (Technically, the explanation is that, in Java, class and array are both considered a “reference” type.)

Also note the syntax “int[]” in int[] myA; and the “int[10]” in myA = new int[10]; The “int[]” and “int[n]” are two unclearly distinguished syntax with differing semantics.

2-Dimensional Arrays

You can create a 2-dimensional Array. (or n-dimensional)

The syntax for n-dimensional array is similar to 1-dimensional array, but with multiple square bracket pairs instead of one.

In the following example, we show the syntax of 3 things:

public class Ar2 {
    public static void main(String[] args) {

        // declaring that myA is a 2-dimensional array
        int[][] myA;

        // give the variable a value, and declare its dimensions.
        myA = new int[3][2];

        // assign a value to a array slot
        myA[0][0] = 6;

        System.out.print(myA[0][0]); // prints the value of a slot
        System.out.print(myA[0].length); // prints the length of a row

Note that the following syntax, which seems logical and regular, is not a valid syntax:

int[] myA; // declare myA to be a array of int
myA = new int[2]; // give myA a value and declare its length

myA[] myB; // declare myB to be another array of “type myA”. (COMPILER ERROR!)
myB = new myA[3]; // give myB a value and declare its length

The technical reason that the above gives a compiler error, is because myA isn't a Java datatype.

Note: list-like things such as general lists, vectors (tuple), keyed-lists (hash table, associative array), and may other types are in the package “java.util.Collections”.

Java Tutorial: arraybasics

Java Lang Spec: arrays

blog comments powered by Disqus