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] + "."); } } |
Join the conversation! Your thoughts help the community grow.