Wednesday, 1 October 2014

Arrays in Java

An array is an object and consequently it is stored in the heap. It is possible to create one or many dimensional arrays. Let's see the various ways to write arrays in Java.

One dimensional Array


int [] scores;      // Ok, recommended
int scores [];      // Ok but not recommended

Creation and assignment:
int [] scores = new int[3];    // Ok, always specify the size
scores[0] = 84;                    // Ok
scores[1] = 19;                    // Ok
scores[2] = 25;                    // Ok

// This compile, however it will throw 
// a runtime exception: "java.lang.ArrayIndexOutOfBoundsException":
scores[3] = 96;

Short-cut assignment:
int [] scores = new int[] {84, 19, 25, 96};   // Ok

// This won't compile: never specify 
// the array's size in a short-cut assignment:
int [] scores = new int[4] {84, 19, 25, 96};   

Short-cut assignment without 'new':
int [] scores = {84, 19, 25, 96}; // Ok

An array of String:
String [] names = {"Tato", "Tuti", "Toto"};    // OK

An array of "Car" objects:
Car [] cars = {new Car("Mercedes"), new Car("Ferrari"), new Car("Renault")};    // Ok

Polymorphism: in this example "Ferrari" extends "Car":
Car [] cars = {new Car("Mercedes"), new Ferrari(), new Car("Renault")};   // OK, as "Ferrari" IS-A "Car"

An array which contains a primitive type can not be casted to an array which contains a different primitive type.
int[] scores;
int[] scoresInt = new int[4];
long[] scoresLong = new long[4];
// Ok, as the variables 'scores' and 'scoresInt' have the same reference type:
scores = scoresInt;
// This won't compile, message: "incompatible types":
scores = scoresLong;

An array which contains an Object type can be casted to an array which contains a different Object type if the condition 'IS-A' is fulfilled.
// In this example, "Horse" extends "Animal".
Animal[] animals = new Horse[4];  // Ok as "Horse" IS-AN "Animal"
Car[] cars = new Car[4];
animals = cars;                   // This won't compile as "Car" IS-NOT an "Animal"
It is possible to cast an array into 'Object':
// Ok, as an "Array" IS-AN "Object":
Object myObj = new int[4];

// This won't compile as this array contains an array of primitive integers:
Object[] myObj = new int[4];

// Ok, as this array contains an array of primitive integers' arrays:
Object[] myObj = new int[3][2];   

Object myObj = new String[4];     // Ok

// Ok, as this array contains an array of String objects:
Object[] myObj = new String[4];

int [][] scores;      // Ok, 2 dimensionnals array
int scores [][];      // Ok, 2 dimensionnals array, but not recommended
int [] scores [];     // Ok, 2 dimensionnals array, but not recommended

int [][] teamsScores = new int[4][2]; // Ok
Object myObj = teamsScores;           // Ok
int[][] scores = (int[][])myObj;      // Ok
int[] scores2 = (int[])myObj;         // This won't compile, the variable 'myObj' has a reference to a 2 dimensional integer array



Multi-dimensional Array


Declaration:
int [][] scores;   // Ok, 2 dimensional array
int scores [][];   // Ok, 2 dimensionnals array, but not recommended
int [] scores [];  // Ok, 2 dimensionnals array, but not recommended

Creation and assignment
int [][] myArray = new int[2][];    // Ok, you do not need to specify the size of the second index
myArray[0] = new int[1];    // Ok
myArray[0][0] = 54;         // Ok
myArray[1] = new int[2];    // Ok
myArray[1][0] = 83;         // Ok
myArray[1][1] = 92;         // Ok


int [][] myArray = new int[2][2];  // Ok, if you specify the second index then you do not need to create new array object for each index
myArray[0][0] = 54;                // Ok
myArray[1][0] = 83;                // Ok
myArray[1][1] = 92;                // Ok

Shortcut assignment:
int [][] myArray = new int [][] {{54}, {83, 92}};  // Ok

Shortcut assignment without 'new':
int [][] myArray = {{54}, {83, 92}};   // Ok