Reputation: 11
I am receiving an error in
numbers[i] = new int[];
Java array dimension missing.
I am practicing loops through an array and stuff how to resolve.
public class Main {
public static void main(String[] args) {
int[] numbers = {10, 35, 17, 95 ,75, 65, 1012, 1, 99, 69};
for (int element : numbers) {
System.out.println(element);
}
for (int i = 0; i < numbers.length; i++) {
numbers[i] = new int[];
}
}
}
Upvotes: 0
Views: 139
Reputation: 253
You have to define the array size and its datatype when creating an array in Java.
int numbers[] = new int[10];
Your loops are correctly written, but the line numbers[i] = new int[];
, is not needed.
If you want to display the array elements you should implement it as given below.
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Upvotes: 1
Reputation: 138
Your first part of the code loops through and prints to screen?
public static void main(String[] args) {
int[] numbers = {10, 35, 17, 95 ,75, 65, 1012, 1, 99, 69};
for (int element : numbers) {
System.out.println(element);
}
Alternatively you could use
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
You don't need the line
numbers[i] = new int[];
You've already declared the array and assigned values to it
Upvotes: 1