Arrays in Java


An Array is a collection of homogeneously-typed variables (same-typed variables) that share a name. Two types of arrays are:
1. One Dimensional Arrays.
2. Multi Dimensional Arrays.

ONE DIMENSIONAL ARRAYS
Declaration Syntax:
Type Variable-Name[];
OR
Type[] Variable-Name;
For e.g.,


Int a[];

Note: The above line of code declares that an array of integer 'a' is declared, however no such array yet exists. Because the array has not been initialized.
An arry must be initialized by using the new keyword, i.e. allocation of memory for the array.
Initialization Syntax:
Variable-Name = new Type[Size];
Now, each element of an array will be initialized to a corresponding value, as in the following table.
Type Value after Initialization
byte 0
short 0
int 0
long 0
float 0.0
double 0.0
char null
boolean false

Example,
a = new Int[5];

In the above example, each element of the array 'a'  is initialized to 0.
A simple program, using Array:
public class JavaArrays {
public static void main(String[] args) {
int DOB[];
DOB = new int[3];
//Above two lines may be written as "int DOB[] = new int[3]".
DOB[0] = 23;
DOB[1] = 6;
DOB[2] = 1990;
System.out.println("My Date of Birth is " + DOB[0] + "-" + DOB[1] + "-" + DOB[2] + ".");
}
}

Output:
My Date of Birth is 23-6-1990.

The same output could be achieved by this easier and simpler version of the above program:
public class JavaArrays {
public static void main(String[] args) {
int DOB[] = {23, 6, 1990};
System.out.println("My Date of Birth is " + DOB[0] + "-" + DOB[1] + "-" + DOB[2] + ".");
}
}

Assigning and Printing values to an array using for loop
public class JavaArrays {
public static void main(String[] args) {
int a[] = new int[4];
for (int i = 0; i < 4; i++) {
// Assigning values to a[]
a[i] = 12 + i;
}
for (int i = 0; i < 4; i++) {
// printing values to a[]
System.out.println(a[i]);
}
}
}

Output:
12
13
14
15

MULTIDIMENSIONAL ARRAYS

Multidimensional arrays are also known as arrays of arrays. Multidimensional arrays have more than one indexes.

Declaration and Initialization Syntax
Type Variable-Name[][]; //Declaration
OR
Type[][] Variable-Name;
Variable-Name = new Type[Size][Size]; //Initialization
Type Variable-Name[][] = new Type[Size][Size]; //Declaration & Initialization
For Example,
public class JavaArrays {
public static void main(String[] args) {

//Allocating a array of 2 by 4 and assigning it to 'a'.

int a[][] = new int[2][4];

int count = 1;

//for loop to fill the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
a[i][j] = ++count;
}
}

//for loop to print the array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}

Output:
2 3 4 5
6 7 8 9

Understanding how the preceding 2-Dimensional Array is filled up:
(0,0) (0,1) (0,2) (0,3)
(1,0) (1,1) (1,2) (1,3)

Left Index shows the row and Right Index shows the column.
Inner for loop runs 4-times for each and every time outer for loop runs, i.e. outer loop runs for rows and the inner loop runs for column.

Up Next
    Ebook Download
    View all
    Learn
    View all