I want to declare an empty array in java and then I want do update it but the code is not working

We Are Going To Discuss About I want to declare an empty array in java and then I want do update it but the code is not working. So lets Start this Java Article.

I want to declare an empty array in java and then I want do update it but the code is not working

Advertisements
  1. I want to declare an empty array in java and then I want do update it but the code is not working

    You are creating an array of zero length (no slots to put anything in)
    int array[]={/*nothing in here = array with no elements*/};
    and then trying to assign values to array elements (which you don't have, because there are no slots)

  2. I want to declare an empty array in java and then I want do update it but the code is not working

    You are creating an array of zero length (no slots to put anything in)
    int array[]={/*nothing in here = array with no elements*/};
    and then trying to assign values to array elements (which you don't have, because there are no slots)

Solution 1

Advertisements

You are creating an array of zero length (no slots to put anything in)

 int array[]={/*nothing in here = array with no elements*/};

and then trying to assign values to array elements (which you don’t have, because there are no slots)

array[i] = number; //array[i] = element i in the array of length 0

You need to define a larger array to fit your needs

 int array[] = new int[4]; //Create an array with 4 elements [0],[1],[2] and [3] each containing an int value

Original Author Ross Drew Of This Content

Solution 2

Advertisements

Your code compiles just fine. However, your array initialization line is wrong:

int array[]={};

What this does is declare an array with a size equal to the number of elements in the brackets. Since there is nothing in the brackets, you’re saying the size of the array is 0 – this renders the array completely useless, since now it can’t store anything.

Instead, you can either initialize the array right in your original line:

int array[] = { 5, 5, 5, 5 };

Or you can declare the size and then populate it:

int array[] = new int[4];
// ...while loop

If you don’t know the size of the array ahead of time (for example, if you’re reading a file and storing the contents), you should use an ArrayList instead, because that’s an array that grows in size dynamically as more elements are added to it (in layman’s terms).

Original Author Mage Xy Of This Content

Solution 3

Advertisements

You need to give the array a size:

public static void main(String args[])
{
    int array[] = new int[4];
    int number = 5, i = 0,j = 0;
    while (i<4){
        array[i]=number;
        i=i+1;
    }
    while (j<4){
        System.out.println(array[j]);
        j++;
    }
}

Original Author brso05 Of This Content

Solution 4

Advertisements

You can try creating new array at every iteration with a size greater than in the previous iteration.
i.e.

public class JavaConversion
{
    public static void main(String args[])
    {
        int array[]=new int[0];
        int number = 5, i = 0,j = 0;
        while (i<4) {
            array = Arrays.copyOf(array, array.length + 1);
            array[i]=number;
            i=i+1;
        }
        while (j<4) {
            System.out.println(array[j]);
        }
    }
}

Original Author Steve Jose Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment