How to Initialize an Array in Java?

In this article we will see how we can initialize an array in Java programming language.

How to Initialize an Array in Java?

As in the previous article we saw that How to declare an array in Java? and How to instantiate an array in Java? now we will see How to initialize an array in Java?

After declaring an array we have the array name and it’s type and after instantiating the array array size is specified and memory is allocated for that array.

When memory is allocated initially ‘0’ is set by default as value of each array index. But after declaration and instantiation of array we need to initialize the elements of the array.

There are multiple ways to initialize an array.

  1. Initializing during declaration
  2. Index wise initialization
  3. Initializing by taking user input

As we know declaration and instantiation already, so directly we will go with examples in case of initialization.

Here, we have demonstrated with integer array.

For one dimensional array:

Approach-1: Initializing during declaration(One dimensional array)

int result[] = {72,63,54,45,78,99,34,66,86,55} //declared, instantiated, initialized

Here directly we are declaring array along with the elements(initialized). So automatically it is instantiated(memory allocated) based on the elements.

Approach-2: Index wise initialization(One dimensional array)

int result[] = new int[10];  //declared and instantiated

int result[0] = 54;  //initialization
int result[1] = 33;
int result[2] = 72;
int result[3] = 64;
int result[4] = 44;
int result[5] = 94;
int result[6] = 88;
int result[7] = 68;
int result[8] = 32;
int result[9] = 73;

We initialized the array elements starting from index-0 to index-9. Assigned elements to all the indices of the array.

Approach-3: Initializing by taking user input(One dimensional array)

int[] result = new int[10];                                         //declared and instantiated
        
for(int i=0; i<result.length; i++)                              //taking input of array elements from user
{  
  result[i]=sc.nextInt();  
}

Here we are taking input of array elements by using for loop and iterating from i=0 to i<result.length
Means from i=0 to i=9. (Here length of array is 10)

For multi dimensional array:

Approach-1: Initializing during declaration(Multi dimensional array)

int result[] = {{72,63,54},{45,78,99},{34,66,86}}  //declared, instantiated, initialized

Here directly we are declaring 3*3 array along with the elements(initialized). So automatically it is instantiated(memory allocated) based on the elements.

Approach-2: Index wise initialization(Multi dimensional array)

int result[] = new int[10];  //declared and instantiated

int result[0][0] = 54;  //initialization
int result[0][1] = 33;
int result[0][2] = 72;
int result[1][0] = 64;
int result[1][1] = 44;
int result[1][2] = 94;
int result[2][1] = 88;
int result[2][1] = 68;
int result[2][2] = 32;

We initialized the 3*3 array elements starting from index-[0][0] to index[2][2]. Assigned elements to all the indices of the array.

Approach-3: Initializing by taking user input(Multi dimensional array)

int[][] result = new int[3][3];                                         //declared and instantiated
        
for(int i=0; i<3; i++)                                   //taking input of array elements from user
{  
  for(int j=0; i<3; j++)   
  {
      result[i][j]=sc.nextInt();  
  }
}

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Articles: