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
How to declare and initialize an empty array of objects
How to create empty array list without array size in java?
Why is it good practice to declare an empty array and then push the items to that 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);
Ok, so i have one method (return statement is int) which tells me how long the array i need to initialise has to be. How can i initialise an empty array with that length which i can then fill ?
i thought something like this is possible, but it's not:
int sum[] = sum[method(x)]