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).
java - How to create an empty array? - Stack Overflow
FAQ: Learn Java: Arrays - Creating an Empty Array
Can we declare empty arrays in Java? - Stack Overflow
Empty Array
You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:
public void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
BufferedReader console =
new BufferedReader(new InputStreamReader(System.in));
while(true) {
String word = console.readLine();
if (word.equalsIgnoreCase("end") {
break;
} else {
numbers.add(Integer.parseInt(word);
}
}
Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example
How about:
Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int size = scan.nextInt();
int[] yourArray = new int[size];
//can even initialize it
Arrays.fill(yourArray, -1);
Arrays have fixed sizes. What you want is called a List.
List<String> newValues = new ArrayList<String>();
newValues.add("hello");
Here's the documentation.
You can do that by:
ArrayList<String> your_list = new ArrayList<String>();
You can add afterwards any number of elements as you want using the .add(...) method. The way you proposed it's not possible since you have to know it's size at declaration time.
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?
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 :)