Java int array

Posted in /  

Java int array
ramyashankar

Ramya Shankar
Last updated on April 20, 2024

    Introduction

    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. Before that, let us explain what is declaration, initialization, and populating array values. Declaring an int array: indicates that we want to create an array of integer values.

    int[] intArray1;

    Initializing: In this step, an initial memory is allotted to the int array that we declared:

    int[] intArray2 = new int[3];

    Populating the array:

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

    Fetching values from the array:

    int val0 = intArray3[0];

    How to declare, initialize and populate int arrays

    int array in Java can be defined in numerous ways. Let us see them one by one with code examples:

    Method 1: To declare the 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 values.

    Method 2: Initialize the array with the initial size and then reinitialize later to the right 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 but later change the 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 the program to work, you need to import java.util.Arrays;

    Method 4: Populate with values while creating the array itself

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

    Note that the above system out statement will give a result as 3+4 = 7.

    Conclusion

    We have introduced some of the ways in which int arrays can be used in Java. Note that we can store integers in a string array as well, and it works in the same way, but if we use int itself, we need not do unnecessary conversions for data manipulations. For example, in method 4, we have added two int array values and got the result as 7. If we add two string array values, we will 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 a result, not 5, as the strings will be appended, not added.

    People are also reading:

    Leave a Comment on this Post

    0 Comments