You are creating an array of zero length (no slots to put anything in)
int array[]={/*nothing in here = array with no slots*/};
and then trying to assign values to array slots (which you don't have, because there are none)
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
Answer from Ross Drew on Stack OverflowYou are creating an array of zero length (no slots to put anything in)
int array[]={/*nothing in here = array with no slots*/};
and then trying to assign values to array slots (which you don't have, because there are none)
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
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).
Is it possible to declare a blank array with multiple spaces?
Empty Array
How can I initialize a String array with length 0 in Java? - Stack Overflow
How to create an empty array with a lenght determined by another method ?
I'm trying to write a method that involves taking an int and using that number to create an array of that size. However, the contents of the array are not known at initialization. Is there a way to declare an array with (for example) ten blank spaces?
I tried
int[] MyArray = new int[Num]; MyArray[Num] = Num;
but that only creates an array with Num at the first index.
Thanks for any help, I really appreciate it :)
I recently learnt that an empty array IN JAVA is filled with "0" not null and I was wondering how you will be able to determine the size of the array if you cant say "i != null " how can you tell how many elements are in the array?
As others have said,
new String[0]
will indeed create an empty array. However, there's one nice thing about arrays - their size can't change, so you can always use the same empty array reference. So in your code, you can use:
private static final String[] EMPTY_ARRAY = new String[0];
and then just return EMPTY_ARRAY each time you need it - there's no need to create a new object each time.
String[] str = new String[0];?