-1

This very basic code prints my 2D array row by row.

public class scratchwork {
    public static void main(String[] args) throws InterruptedException {
        int[][] test = new int[3][4];

        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 4; col++) {
                System.out.print(test[row][col] = col);
            }

            Thread.sleep(500);
            System.out.println();
        }
    }
}

How can I edit the loops to print the array column by column?

Edit: just want to clarify that the output is

0123

....

0123

....

0123

with the dots representing not actual white space but the half second sleep time. What I'm trying to output is

0...1...2...3
0...1...2...3
0...1...2...3

So I am trying to print the columns a half second apart from each other.

  • Flip the for loops! Columns loop first, then row loop as inner. – Ashwin Gupta Apr 05 '16 at 22:13
  • HW. write down the array on a piece of paper. Mark the indices. Write out the indices you need to get for a column by column iteration. Then figure out how to setup your loops. – gidim Apr 05 '16 at 22:14
  • What you want to do it is [***transpose***](http://stackoverflow.com/questions/26197466/transposing-a-matrix-from-a-2d-array) the grid, matrix, etc.... – Mr. Polywhirl Apr 05 '16 at 22:16
  • I tried flipping the for loops but the output is not what I need. When I say column by column I mean print the same array but printed from left to right, which is why I have the Thread.sleep – seventeenhundred Apr 05 '16 at 22:18
  • What sort of output are you expecting, what does your input look like? Provide some examples so we can see what it is your asking. All we see is code that prints 2D rows so are answer is to flip it, but if your seeking specific output, then you need to provide us with some examples so we can see what your problem is. – Senglish Apr 05 '16 at 22:23
  • I made an edit hoping to clarify myself – seventeenhundred Apr 05 '16 at 22:31
  • If you are trying to print THREE lines at a time and update the output, you will need to build the string and print out the updated text each time... Is this what you are asking? http://pastebin.com/4YNxe9KV – Mr. Polywhirl Apr 05 '16 at 22:46
  • Yes, that's exactly what I was asking. I had not tried strings yet. So a rough idea would making each row a string and using thread.sleep by character? – seventeenhundred Apr 05 '16 at 22:56

2 Answers2

0

You just need to change the order of the nested loops. If you want to print them columns at a time, then columns need to be the outermost looping variable.
Just consider that the inner loop will be executed multiple times per outer loop.

jbord39
  • 169
  • 1
  • 10
  • I think I caused some misunderstanding by not being specific enough. My edit shows what I meant originally – seventeenhundred Apr 05 '16 at 22:40
  • If you want the '...' to go between 1234 put it innermost loop. Right now it is in the outermost loop, so it happens less often. If you want it to output ... every count, Thread.sleep(500); underneath the System.out.print(test[row][col] = col); also you will need to check that the looping variable is not the last one, or you will get 0....1....2....3.... instead of 0...1...2...3 – jbord39 Apr 05 '16 at 23:20
0

If you want to print out each column on a timer, then you will need to use three loops.

You will need to clear out the previous console output for each iteration. The following code works in Windows if you execute the program through the command line. Of course, this is platform specific, but there are many helpful answers, here on Stack Overflow, and other sites to help you clear console output.

import java.io.IOException;

public class ThreadSleeper {
    public static final int TIMEOUT = 500;
    public static final int ROWS = 3, COLS = 4;
    static int[][] test = new int[ROWS][COLS];

    public static void main(String[] args) {
        // Populate values.
        for (int i = 0; i < ROWS * COLS; i++) {
            test[i / COLS][i % COLS] = i % COLS;
        }
        try {
            printColumns();
        } catch (InterruptedException | IOException e) {
            e.printStackTrace();
        }
    }

    public static void printColumns() throws InterruptedException, IOException {
        for (int counter = 0; counter < COLS; counter++) {
            clearConsole(); // Clearing previous text.
            System.out.printf("Iteration #%d%n", counter + 1);
            for (int row = 0; row < ROWS; row++) {
                for (int col = 0; col <= counter; col++) {
                    System.out.print(test[row][col] + "...");
                }
                System.out.println();
            }
            Thread.sleep(TIMEOUT);
        }
    }

    // http://stackoverflow.com/a/33379766/1762224
    protected static void clearConsole() throws IOException, InterruptedException {
        new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132