sneppets-java8

How to declare and initialize arrays in Java Programming ?

This tutorial guides you on how to declare and initialize arrays in Java Programming. When you declare and create Arrays, they are created on dynamic memory by JVM. 

Declare and Initialize Arrays

An array represents a group of elements of same data type. So we can store group of elements of same data type and cannot store group of elements in a array of different data types. As said earlier arrays are created on dynamic memory only in Java.

Arrays are generally categorized into two types, they are single dimensional and multi dimensional arrays.

Single dimensional arrays

Single dimensional arrays represents a row or a column of elements. Let’s see how to declare and initialize one dimensional array.

Way 1

The below example shows one way to declare one dimensional array and initialize (store group of elements) at the time of its declaration.

int studMarks[] = {75, 55, 80, 95, 60, 45}

In the above example, “int” represents integer type of elements are stored in the array “studMarks” . And pair of square braces “[]” represents that it is one dimensional array. Then the actual integer elements are mentioned inside the curly braces “{ }”.

For the above line of code JVM will create 6 blocks of memory as there are 6 elements that need to be stored in the array. These blocks of memory can be referred as studMarks[0], studMarks[1], studMarks[2], …..studMarks[5]. Here 0,1,2,3,4,5 is called index of the array.

Note, a one dimensional array will have only one index. So in general we can represent the any element of the array as studMarks[i].

Way 2

Another way to declare and initialize arrays is by declaring the array first and then allot memory by using new operator. Finally store elements as shown in the example below.

int studMarks[]; // declare the array

studMarks = new int[6]; // allot memory to store 6 integer elements

studMarks[0] = 75; //store elements
studMarks[1] = 55;
studMarks[2] = 80;
studMarks[3] = 95;
studMarks[4] = 60;
studMarks[5] = 45;

Note, the two statements in the above code sneppet i.e., the declaration and memory allocation can be combined ans written as single statement as shown below.

int studMarks[] = new int [6];

Way 3

Using IntStream (java.util.stream) toArray() method you can declare and initialize arrays as shown in the following example.

int studmarks[]= IntStream.of(75, 55, 80, 95, 60, 45).toArray();

Alternate way to declare and initialize arrays

As mentioned in the following examples, the pair of square braces “[]” can be written before or after the array name. It’s one and the same.

int studMarks[] = {75, 55, 80, 95, 60, 45} 
(OR)
int[] studMarks = {75, 55, 80, 95, 60, 45}

int studMarks[] = new int[6];
(OR)
int[] studMarks = new int[6];

Multi dimensional Arrays

Multi dimensional arrays represent 2D, 3D, etc., arrays. Let’s see how to declare and initialize two dimensional arrays.

Way 1

Two dimensional arrays can be declared and initialized at the time of declaration as shown in the example below.

int studMarks[][] = { {40,60,80,90,50,70},
                      {80,90,65,50,45,55},
                      {35,55,77,87,66,88}}

In the above example, “int” represents integer type of elements are stored in to the array and “studMarks” is the array name. For two dimensional arrays we should use two pairs of square braces “[][]”. And each row of elements are written inside the curly braces “{}” and the rows and the elements in the each row are separated using commas “,”.

In our example there are 3 rows and 6 columns. So JVM creates 3 x 6 = 18 blocks of memory which is generally represented as studMarks[i][j].

Way 2

Another way to declare and initialize two dimensional array is by declaring the array first and then do memory allocation for the array using new operator as shown in the example below.

int studMarks[][]; //declare the array

studMarks = new int[3][6]; //memory allocation

(OR)

//The above two statements can be combined to a single statement

int studMarks[][] = new int[3][6];

The following example shows alternate ways to declare and initialize two dimensional arrays.

int studMarks[][] = new int[3][6]; 
(OR)
int[][] studMarks = new int[3][6];

Also See:

References:

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments