0
What is error in it???
mport java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("Enter the length of array:"); Scanner scan = new Scanner(System.in); int len = scan.nextInt(); int[] arr={}; for(int i=0;i<=(len-1);i++) { System.out.print("Enter the numbers one by one:"); int a = scan.nextInt(); arr[i]=a; } System.out.print(arr); } }
2 Antworten
+ 3
1. You're missing the i at the beginning of import.
2. You aren't initializing the array correctly. Try;
int[] arr = new int[len];
3. System.out.print(arr);
Will output the memory location of the the array. Use;
for (int n : arr) {
System.out.println(n);
}
To output each element of the array instead.
Or if you want to output so it looks like the array.
System.out.println(Arrays.toString(arr));
Note: you'll need the import;
import java.util.Arrays;
+ 1
in addition to what ChaoticDawg mentioned, if you need a resizable Array, use ArrayList instead of primitive array data type.