News & UpdatesProgrammingWeb programming StoreMy Projects

Java Tutorial – 07 – Arrays

An array is a data structure used for storing a collection of values.

Array declaration

To declare an array, a set of square brackets is appended to the data type the array will contain, followed by the array’s name. Alternatively, the brackets may be placed after the array name. Arrays can be declared with any data type and all of its elements must then be of that type.

int[] x;
int y[];

Array allocation

The array is allocated with the new keyword, followed again by the data type and a set of square brackets containing the length of the array. This is the fixed number of elements that the array can contain. Once the array is created, the elements will automatically be assigned to the default values for that data type.

int y[] = new int[3];

Array assignment

To fill the array elements they can be referenced one at a time, by placing the element’s index inside the square brackets, and then assigning them values. Notice that the index starts with zero.

y[0] = 1;
y[1] = 2;
y[2] = 3;

Alternatively, the values can be assigned all at once by using a curly bracket notation. The new keyword and data type may be optionally left out if the array is declared at the same time.

int[] x = new int[] {1,2,3};
int[] x = {1,2,3};

Once the array elements are initialized, they can be accessed by referencing the elements’ indexes inside the square brackets.

System.out.print(x[0] + x[1] + x[2]); // "6"

Multi-dimensional arrays

Multi-dimensional arrays are declared, created and initialized much like one-dimensional arrays, except that they have additional square brackets. They can have any number of dimensions, and for each dimension another set of square brackets is added.

String[][] x = {{"00","01"},{"10","11"}};
String[][] y = new String[2][2];
y[0][0] = "00";
y[0][1] = "01";
y[1][0] = "10";
y[1][1] = "11";
System.out.print(x[0][0] + x[1][1]); // "0011"

ArrayList class

An important thing to keep in mind about arrays is that their length is fixed and there are no methods available to change their size. In fact, the only array member that is regularly used is length, to obtain the size of the array.

int x[] = new int[3];
int size = x.length; // 3

For cases when a resizable array is needed the ArrayList class can be used, which is located in the java.util package. Items in the ArrayList are stored as the generic Object type. The ArrayList can therefore hold any data types, except for primitives.

// Create an Object ArrayList collection
java.util.ArrayList a = new java.util.ArrayList();

The ArrayList class has several useful methods to change the array, including: add, set and remove.

a.add("Hi");       // add an element
a.set(0, "Hello"); // change first element
a.remove(0);       // remove first element

To retrieve an element from the ArrayList the get method is used. The element then has to be explicitly cast back to its original type.

a.add("Hello World");
String s = (String)a.get(0); // Hello World
Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours