I have been struggling with the following problem:
I am trying to print the below output using nested for loops and two dimensional arrays.
int[][] outputArray = {
{1,2,3,4,5,6,7,8,9,10},
{11,12,13,14,15,16,17,18,19,20},
{21,22,23,24,25,26,27,28,29,30},
{31,32,33,34,35,36,37,38,39,40},
{41,42,43,44,45,46,47,48,49,50},
{51,52,53,54,55,56,57,58,59,60},
{61,62,63,64,65,66,67,68,69,70},
{71,72,73,74,75,76,77,78,79,80}
};
Here is my code for the array which seems to be correct:
public ExerciseTwo() {
myArray1 = new int[8][10];
for (int i = 0; i < myArray1.length; i++) {
for (int j = 0; j < myArray1[i].length; j++) {
myArray1[i][j] = (i * myArray1[i].length) + j + 1;
}// end inner loop
}// end outer loop
}// end constructor
Now I am having several issues with the nested loop below:
public void printArrayStatement() {
System.out.print("int[][] outputArray = {");
for (int i = 0; i < myArray1.length; i++) {
if (myArray1.length >= 1)
// I am trying to remove the initial comma here but my
// logic is wrong. It is printing 1 first on each line.
System.out.print("\n" + "{" + myArray1[0][0]);
for (int j = 0; j < myArray1[i].length; j++) {
System.out.print("," + myArray1[i][j]);
}
}
System.out.println("};");
}// end method
I also can't seem to figure out how to get the }, at the end of each line. I think an if statement is necessary but I can't figure out the code!