简体   繁体   English

逐列打印二维数组

[英]Print a 2D array column by column

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.所以我试图打印列彼此相隔半秒。

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.只需考虑内循环将在每个外循环中执行多次。

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.如果您通过命令行执行程序,以下代码在 Windows 中有效。 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.当然,这是特定于平台的,但在 Stack Overflow 和其他网站上有许多有用的答案,可帮助您清除控制台输出。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM