How to Initialize an Array in Java

14 views

In this article, we’ll go through how you can initialize an array in java, through detailed examples.

1. How to Initialize a 1D Array

To create a one-dimension array in Java, the formula is the following:

Object_type_or_primitive[] array_name =  
new Object_type_or_primitive[int_or_short_value]

For example, we could initialize an array of type Person and length 3, with the following command:

Person[] persons = new Person[3];

In addition, we could use primitive types, so to create an array of type short and length 100, the command is the following:

short[] numbers = new short[100];

Should you like to declare and initialize an array in one command, you could use the following formula:

Object_type_or_primitive[] array_name =  
new Object_type_or_primitive[] { value_1, value_2, value_3, ..., value_n}

So, to initialize the array in section 1, the command is the following:

String[] names = 
new String[] {"Akis", "Giorgos", "Nikos", "Dimitris", "Giannis", "Kostas"};

2. How to Initialize a 2D Array in Java

To initialize a 2d array, the formula is the following:

Object_type_or_primitive[][] array_name =  
new Object_type_or_primitive[int_or_short_value_rows] [int_or_short_value_columns]

For example, we could initialize an array of type Person with 3 rows and 3 columns, with the following command:

Person[][] persons = new Person[3][3];

Should you like to declare and initialize a Java array in one command, you could use the following formula:

Object_type_or_primitive[][] array_name =  
new Object_type_or_primitive[][] { {value_1,value_2}, {value_3,value_4}, ... , {value_n,value_m}}

So, to create a 3 X 3 with Xs and Os (like tic-tac-toe), we would write the following:

String[][] names = new String[][] {
  {"X", "X", "X"},
  {"O", "X", "O"},
  {"O", "X", "O"}
};

3. How to Initialize a 3D Array in Java

To initialize a 3d array, the formula is the following:

Object_type_or_primitive[][][] array_name =  
new Object_type_or_primitive[int_or_short_value_rows] [int_or_short_value_columns] [int_or_short_value_columns]

For example, we could initialize an array of type int with a total size of 27 with the following command:

Person[][][] persons = new Person[3][3][3];

Should you like to declare and initialize a java array in one command, you could use the following formula:

Object_type_or_primitive[][] array_name = new Object_type_or_primitive[][] 
{
  { 
     {value_1,value_2}, {value_3,value_4}, ... , {value_n,value_m}
  },
  { 
     {value_1,value_2}, {value_3,value_4}, ... , {value_n,value_m}
  },
  { 
     {value_1,value_2}, {value_3,value_4}, ... , {value_n,value_m}
  },
...
}

So, to create a int array of size 3 X 3 X 3 with numbers, we would write the following:

int [][][] numbers = new int[][][]{
        {
                {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
        },
        {
                {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
        },
        {
                {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
        },
};

4. Conclusion

By now you should be able to initialize a 1D, 2D, or even a 3D array either by just declaring the size of it or by filling it during initialization. To learn everything about Java Arrays, you can click here.

Related Posts