int[] array = new int[scan.nextInt()];
I found this code online and don't understand it. I think it dynamically creates an array. Can someone explain what's going on?
int[] array = new int[scan.nextInt()];
I found this code online and don't understand it. I think it dynamically creates an array. Can someone explain what's going on?
int[] array = new int[scan.nextInt()];
is equivalent to the following code snippet.
int size = scan.nextInt();
int[] array = new int[size];
The code snippet creates an array with the size given as an input by the user. You can check it by printing the length of the array.
System.out.println(array.length);
int[] array = new int[scan.nextInt()];
int - is the type of elements that will be contained within the array.
[ ] - indicates that it's an array.
array - reference to the array object.
new - creates the array object.
scan.nextInt() - scans the next token of the input as an int and sets the array length to that value.