How to Initialize an Array in Java with Example

In this tutorial, we will learn how to initialize array in Java with the help of an example. An array is a significant part of one of the major data structures in Java, which is especially useful in solving a lot of problems in all programming languages.

What is an Array?

For the definition of an array it is a set of data within the same type.

In addition, an array is generally declared with several values, which are stored in the same memory. Similarly, variables are where we can store only one value in memory.

Furthermore, an array allows one to make one variable that keeps different values together. Rather than declaring a variable for each value.

The position of a special data point in the array is known as an index, while the data itself is called an element.

We will be using the NETBEANS IDE 8.2 for writing a program code. Also, we can use any IDE of our own choice.

How to Declare and Initialize an Array in Java?

We can declare and initialize an array in Java in two steps. The first process is the new keyword, which is to initialize the values one by one. The second process which is to put a values in a curly braces.

Initializing an array in Java with the new keyword?

We can declare the array with the use of the syntax below:

dataType [ ] DescriptionOfArray;
  • dataType: This is the type of data we want to put in the array. It should be a string, integer, double, and etc.
  • [ ]: It defines that the variable to declare will consist of an array in any values.
  • nameOfArrary: This to define as an array identifier.

In the above details, we only declared an array and we should initialize it.

We will use syntax for initializing an array in Java.

dataType [] descriptionOfArray = new dataType [size]

The size is regularly shown with a numeric value. It will suggest if how many values to hold in the array. Its value is inflexible, in other words, it will be able to put more than the number stated as the size in the array.

Let’s look at an example below:

package com.kolade;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {

String [] country = new String[3];
country[0] = "Philippines";
country[1] = "China";
country[2] = "Russia";
   }
}

In the example program above, we initialized an array of strings, which is the country; the other term is called an identifier. So the size is three, and it can only handle three values.

It contains three indexes:

  • The value of the Philippines is in index 0
  • The value of the China is in index 1
  • The value of the Russia is in index 2

Don’t be distracted by the numbers 0, 1, and 2. The zero-indexed is an array, As a result, the counting begins at 0, not in 1.

Moreover, In the array example above, if we add the extra data. For example, we will add the country[3] = "Ukraine“. It will get an error because we have defined that the array should consist only contain 3 values. If we want to add extra values, we have to increase the size of the array.

Let’s look at the snippet below which is the result is error:

Initializing an array in Java

We will print the array to the console of NETBEANS IDE 8.2. We will use the built-in toString() method to print an array:

System.out.println(Arrays.toString(country));

Let’s look at the complete example code with an output result.

package com.kolade;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
   // write your code here
String [] country = new String[3];
country[0] = "Philippines";
country[1] = "China";
country[2] = "Russia";

System.out.println(Arrays.toString(country));
   }
    
}

Output:

Initialize an array in Java in one line

We can initialize an array in Java in one line with the used of basic syntax below:

dataType [ ] typeOfArray = {value1, value2, value3, value4}

Within this method, we don’t need to define the size of the array, we can put as many values as we like in it.

We will look at the example below:

package com.kolade;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
   // write your code here
String [] places = {"Manila", "Negros Occidental", "Cebu", "Bulacan", "Pampanga"};

System.out.println(Arrays.toString(places));
   }
    
}

Output:

Initialize an array in Java

How to Loop Through an Array in Java

We can loop through an array in Java with the for loop and increase the for loop. The for loop will provide the index of each value, yet the compliment for loop it is not.

Array in for Loop Java

In Java language, we can use the for loop with the used of basic syntax below:

for (dataType i = 0; x< ListOfArray.length; x++) {
// Code to execute
}

Then we can loop through the namesTwo array as follows:

package com.kolade;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
   // write your code here

        String [] country = {"Singapore", "Iran", "Saudi", "Iraq", "Dubai"};

        for (int i = 0; i < country.length; i++) {
            System.out.println("The country at index " + i + " : " + country[i]);
        }
    }
}

Output:

Array in for Loop Java

How do you loop around an array using enhanced for loop?

The modified for loop is an advanced version of the for loop in Java. The disadvantage is that you cannot access the index of the individual values in the array.

We will use the basic syntax of the advanced for loop looks like this below:

for (dataType values: valueOfArray) {
    // write your code here
}

Let’s look at the example below:

package com.kolade;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
   // write your code here

        String [] languages = {"Java Language", "PHP Language", "C Language", "Python Language", "JavaScript Language"};

        for (String programming : languages) {
            System.out.println(programming);
        }
    }
}

Output:

How do you loop around an array using enhanced for loop

Conclusion

In this tutorial, we already discussed How to Initialize an Array in Java in two various ways with the new keyword and with using curly braces.

We also discussed how to loop through arrays in Java with the use of for loop and advanced for loop.

Thank you for taking your time reading this, and please keep coding.

Leave a Comment