Is it possible to create an array with different sizes of columns using Java scanner? like = [[1, 2][3, 4, 5, 6][7, 8, 9]] I've tried it but It doesn't work when the variable element is outside the loop next to it, but when it's inside the loop next to it, it seems working. Here's the code:
import java.util.Scanner;
/**
*
* @author cismvillanueva
*/
public class FindInGroup {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder output = new StringBuilder();
System.out.print("Enter number of groups: ");
int groups = scanner.nextInt(); //number of groups
System.out.print("Enter number of elements to be search: ");
int nSearch = scanner.nextInt();
int[] nElements = new int[groups];
for (int i = 0; i < groups; i ) {
System.out.print("Enter number of elements for each group: ");
nElements[i] = scanner.nextInt();
}
int[][] elements = new int[groups][nElements[i]];//here's the variable that gives error when outside the next loop
for (int i = 0; i < groups; i ) {
for (int j = 0; j < nElements[i]; j ) {
System.out.print("Enter number: ");
elements[i][j] = scanner.nextInt();
output.append("\n").append(elements[i][j]);
}
System.out.println(" ");
}
}
}
CodePudding user response:
As you know the size of the inner arrays only in the loop, that is where you may create them
int[][] elements = new int[groups][];
for (int i = 0; i < groups; i ) {
System.out.format("Values of group %d\n", i 1);
elements[i] = new int[nElements[i]];
But then you see you're iterating twice over values [0;groups[
you could do all in one loop, not the it changes the order of input : values are asked just after each group size
int[][] elements = new int[groups][];
for (int i = 0; i < groups; i ) {
System.out.printf("Enter number of elements for group %d: ", i 1);
elements[i] = new int[scanner.nextInt()];
for (int j = 0; j < elements[i].length; j ) {
System.out.print("Enter value: ");
elements[i][j] = scanner.nextInt();
}
}
System.out.println(Arrays.deepToString(elements));
Enter number of groups: 3
Enter number of elements for group 1: 2
Enter value: 1
Enter value: 2
Enter number of elements for group 2: 4
Enter value: 3
Enter value: 4
Enter value: 5
Enter value: 6
Enter number of elements for group 3: 3
Enter value: 7
Enter value: 8
Enter value: 9
[[1, 2], [3, 4, 5, 6], [7, 8, 9]]
CodePudding user response:
A 2D array is just an array whose element are arrays too - an array is an instance as any other object.
Example, if you have an 2D array declared as int[][] elements
, you could do int[] innerArray = elements[i]
.
Avoiding too much changes to your original code, you must first create the outer array, before the last loops:
int[][] elements = new int[groups][]
this will create an array of groups
elements, each being able to hold a int
array, but not yet initialized, that is, all null
.
Inside the outer loop, you must create the inner array before populating that inner array, that is, before the inner loop:
elements[i] = new int[nElements[i]];
The second loop (over j
) can now set each element (elements[i][j]
).
This can be optimized, as given in azro's answer, there is no need for nElements
- you can directly create the inner arrays without saving the size into an additional array. This is a bit faster, but also easier to read (IMO) and one possible error-source less.