29

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

public class JavaConversion
{
    public static void main(String args[])
    {
        int array[]={};
        int number = 5, i = 0,j = 0;
        while (i<4) {
            array[i]=number;
            i=i+1;
        }
        while (j<4) {
            System.out.println(array[j]);
        }
    }
}
5
  • 3
    You need to give the array a size...
    – brso05
    Jan 18, 2016 at 16:18
  • Creating an empty array means that all slots are empty, BUT you have at least to provide the number of slots.
    – Arnaud
    Jan 18, 2016 at 16:23
  • 2
    When declaring an array, it's best practice to place the brackets directly after the type, i.e. int[] array.
    – Jonathan
    Jan 18, 2016 at 16:35
  • I want an array whose size is not constant Jan 19, 2016 at 1:42

8 Answers 8

35

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
2
  • I tried this, but I get an error saying "Generic array creation" when I do SomeClass array[] = new SomeClass[4];. Aug 12, 2021 at 9:10
  • @DonaldDuck it sounds like your SomeClass is a generic type (e.g. SomeClass<SomeOtherClass>) which isn't supported for arrays in Java.
    – Ross Drew
    Aug 13, 2021 at 20:39
15

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).

9

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++;
    }
}
3

So the issue is in your array declaration you are declaring an empty array with the empty curly braces{} instead of an array that allows slots.

Roughly speaking, there can be three types of inputs:

  1. int array[] = null; - Does not point to any memory locations so is a null arrau
  2. int array[] = {} - which is sort of equivalent to int array[] = new int[0];
  3. int array[] = new int[n] where n is some number indicating the number of memory locations in the array
2

You can't set a number in an arbitrary place in the array without telling the array how big it needs to be. For your example: int[] array = new int[4];

1
  • <1 minute too slow ;) Jan 18, 2016 at 16:21
2

You can do some thing like this,

Initialize with empty array and assign the values later

String importRt = "23:43 43:34";
if(null != importRt) {
            importArray = Arrays.stream(importRt.split(" "))
                    .map(String::trim)
                    .toArray(String[]::new);
        }

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

Hope it helps..

2

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]);
        }
    }
}
2

It is better for that operation use ArrayList

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.