How to Declare, Initialize and Populate Java int Arrays?

Posted in /  

How to Declare, Initialize and Populate Java int Arrays?
ramyashankar

Ramya Shankar
Last updated on March 28, 2024

    In Java, int arrays are used to store integers. In this article, we will see the different ways in which we can create and manage integer or int arrays. However, before that, let us explain what is declaration, initialization, and populating array values in Java. Declaring an int array indicates that we want to create an array of integer values.

              int[] intArray1;

    In initialization, an initial memory is allotted to the int array that we declared.

              int[] intArray2 = new int[3];

    Populating the array means storing values in the array.

              intArray3[0] = 10;
              intArray3[1] = 20;
              intArray3[2] = 30;

    We can fetch values from the arrays like this:

              int val0 = intArray3[0];

    How to Declare, Initialize and Populate Java int Arrays?

    There are many ways to define an int array in Java. Let us see them one by one with code examples.

    Method 1: To declare the Java int array and then later populate and use it when required.

         int[] intArray1;
            //few lines of code here, for example to determine the size of array
         int size = 5;
         intArray1 = new int[size];
         int j = 0;
         for(int i=0; i<size; i++){
         intArray1[i] = ++j;
         System.out.println(intArray1[i]);
         }

    In the above method, we declare the array without initializing it. Later in the code, we initialize the array with the mentioned size and add the values.

    Method 2: Initialize the array with an initial size and then reinitialize later to the required size.

             int[] intArray2 = new int[3];
             Random random = new Random();
             int randSize = random.nextInt(10);
             System.out.println(randSize);
             intArray2 = new int[randSize];
             j = 0;
             for(int i=0; i<randSize; i++){
             intArray2[i] = ++j;
             System.out.println(intArray2[i]); 
             }

    In this method, we initialize the array with some initial size and later change the size to the required size. We are using a for loop to fill the values. You have to add import java.util.Random; for the code to work.

    Method 3: Initialize the array with the correct size and assign individual values.

             int[] intArray3 = new int[5];
             intArray3[0] = 10;
             intArray3[1] = 20;
             intArray3[2] = 30;
             intArray3[3] = 40;
             intArray3[4] = 50;
             System.out.println(Arrays.toString(intArray3));

    Note that for this program to work, you need to import java.util.Arrays;

    Method 4: Populate with values while creating Java int arrays.

             int[] intArray4 = {1,2,3,4,5};
             System.out.println(intArray4[2]+intArray4[3]);

    The above system.out statement will give a result, 3+4 = 7.

    Conclusion

    We have discussed the various ways to declare, initialize, and populate Java int arrays. Note that we can store integers in a string array as well, and it works in the same way, but if we use int, we don't need to do unnecessary conversions for data manipulations. For example, in method 4, we have added two int array values and got 7 as the result. If we were to add two string array values, we would get a different answer:

              String[] strArray = {"1", "2", "3", "4", "5"};
              System.out.println(strArray[1]+strArray[2]);

    Note that we will get ‘23’ as the result and not 5, as the strings will be appended due to operator overloading, not added. People are also reading:

    Leave a Comment on this Post

    0 Comments