I have the following two dimensional array in my java program.
Integer[][] arrayOfSets = {
{1,2,5,6,9,10},
{9,10,11,12},
{1,2,3,4},
{3,5,6,7,8},
{9,10,11,12},
{4,8},
};
Can someone show me the code on how to make the program dynamic, where the program asks the user to enter the row number and column number, i.e. how many arrays the two dimensional array should hold and then asks the user for how big each array and what are the numbers that each array should hold? Thanks so much in advance.
This is what I've tried so far:
System.out.println("HOw many sets arrays would you like");
int numArrays=sc.nextInt();
Integer [][] arrayName = new Integer[numArrays][];
for(int i=0;i<arrayName.length;i++){
System.out.println("enter the size of the array number"+i);
int sizeArray=sc.nextInt();
for(int k=0;k<sizeArray;k++){
System.out.println("enter element");
int e=sc.nextInt();
arrayName[i][k]=e;
}
}
Does this look right? Thanks for all the help in advance.
Ok I modified my cod again. Following is what I have now:
System.out.print("How many arrays would you like?"); int numSets=sc.nextInt(); Integer[][] result = new Integer[numSets][];
for (int k=0; k< numSets; k++){
System.out.println("Enter the size of the array");
int setSize = sc.nextInt();
Integer[] row = new Integer[setSize];
for (int m=0; m< setSize; m++){
System.out.println("enter element: ");
row[m] = sc.nextInt();
}
result[k] = row;
}