I have a question that I am trying to answer for a class I am taking (yes this is a homework question - an extra credit one) but I do not want someone to simply give me the answer. I would much prefer someone to gently point me in the right direction to what I should (or shouldn't) be doing to solve this.
This class is my first exposure to Java, so if my code and understanding comes across as terrible that would be why, so I apologize in advance.
The problem:
I need to create an array 3 rows by 5 columns by 3 sets. My out put would resemble:
Set 1:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
Set 2:
15 16 17 18 19
20 21 22 23 24
etc, etc.
The code I was able to figure out (so far) produces the range I need for the 3 sets as a whole, 0-44, but I am not sure how to format these so it looks like the above. When I run the code I just have one long list going from 0-44.
Could anyone point me in the right direction as to what I should be doing to break this lists into the rows/columns I need to without simply handing me the answer? I want to be able to say that I figured it out on my own, just had to ask for a little direction to get there.
My code:
public static void main(String[] args) {
int list[][][] = new int [3][5][3]; //create rows, column, units (buildings) array structure.
int i,j,k; //assign variables for row, column and unit (building).
int ctr = 0; //set counter.
//create array
for (i=0; i<3; i++)
for (j=0; j<5; j++)
for (k=0; k<3; k++)
{list[i][j][k] = ctr++;}
for (i=0; i<3; i++)
for (j=0; j<5; j++)
for (k=0; k<3; k++)
//Format array
{System.out.println(list[i][j][k] + " ");}
}
EDIT:
So some progress here thanks to everyone's suggestions. I am able to format this in the overall way needed of 5 columns of data. I realized (fumbling through this) that my set up for the original "int list [] [] []" was incorrect, which is why I was having problems with this formatting the way I needed it to correctly (column and row counts).
The next question I would have, if I can ask, would be how I would go about inserting the "Set X" text where needed? I was able to figure out the Set 1 placement, but I would like to (now) have the words "Set 2" appear before the row which begins with 15 and the words "Set 3" before the row which starts with 30.
Would I want to create separate loops for each of the value ranges that each "Set X" chunk would be (range wise) and then just label them accordingly possibly?
My revised code at this point is as below (and hopefully formatted nicer than before). Again, not looking for anyone to hand me an answer, just hoping to get some direction on what to look for in order to figure this out.
public class ECThreeD {
public static void main(String[] args) {
int list[][][] = new int[3][3][5]; // create rows, column, units
// (buildings) array structure.
int i, j, k; // assign variables for row, column and unit (building).
int ctr = 0; // set counter.
// create array
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 5; k++) {
list[i][j][k] = ctr++;
}
}
}
System.out.println("Set 1");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
for (k = 0; k < 5; k++) {
// Format array
System.out.print(list[i][j][k] + " ");
}
System.out.println();
}
}
}
}