Jason C
Jason C

Reputation: 21

Java Array Loop

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();
            }
        }
    }
}

Upvotes: 1

Views: 218

Answers (3)

Irina Rapoport
Irina Rapoport

Reputation: 1692

  1. It would help you (and the readers) understand your logic if instead of i, j, k you used meaningful variables, such as row, column, set.

  2. Your formatting is.. interesting! It's not bad, it's just not how one usually thinks in Java. I can understand what you wrote, but I can tell you are thinking in a different language.

I pasted your code to Eclipse and did auto-format (Ctrl-Shift-F), and here is the result:

    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] + " ");
                }
    }

I am not trying to nit-pick, but this is a necessary step in the right direction, before you can add a bit more complexity to this code. Also, your professor probably expects a similar format.

  1. As you probably know, println adds a newline, while print does not. So the first step would be to use print, not println, in the most general case; and use println only when you actually need to move to a new line. Since you asked for just a hint, I'll leave it at that. Good luck!

Upvotes: 1

Ghostkeeper
Ghostkeeper

Reputation: 3050

You need to print new lines in between, using System.out.println(). Also, println() always prints a new line (as if pressing Enter) after the string. There's also System.out.print(), which doesn't put a new line after it.

Think about when you need the new lines. After every row or column? Perhaps an extra one after every set to make an empty line.

EDIT: I've indented your code properly. It'll be a bit easier to see where to add code then.

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++) { //Prints 3 sections.
    for (j=0; j<5; j++) { //Prints 5 rows.
        for (k=0; k<3; k++) { //Prints 3 columns.
            //Format array
            System.out.println(list[i][j][k] + " ");
        }
    }
}

Upvotes: 1

Filipe Gon&#231;alves
Filipe Gon&#231;alves

Reputation: 21233

First, some indentation:

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] + " ");
            }
        }
    }
}

Please try to structure your code like this, it's easier to read.

Consider what happens in the last set of loops. You want to print a newline after every row, so perhaps you will need to add further print calls outside of the inner loop ... you need a newline after printing a row. It's easy, just read this carefully.

Upvotes: 0

Related Questions