简体   繁体   English

如何在ZigZag路径中遍历和打印2D数组

[英]How To Traverse And Print A 2D Array In ZigZag Path

Suppose I have an array 假设我有一个数组

A[][] = {{ 1, 2, 3, 4},
         { 5, 6, 7, 8},
         { 9,10,11,12}};

And I want to print a wave so that the output comes out like this 我想打印一个波形,以便输出像这样

{1,5,9,10,6,2,3,7,11,12,8,4}

How can I do this?? 我怎样才能做到这一点??

here is my code but it is giving me ArrayIndexOutOfBound 这是我的代码,但它给了我ArrayIndexOutOfBound

public class Wave1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
    System.out.println("Row "+a.length);
    for(int i=0;i<a.length;i++){
        System.out.println("Column "+i+"th "+a[i].length);
    }
    for(int i=0;i<a.length;i++){
        for(int j=0;j<a[i].length;j++){
            System.out.print(a[i][j]+" ");
        }
    }
    System.out.println();
    for(int i=0;i<a.length+1;i++){
        if(i%2==0){
        for(int j=0;j<a.length;j++){
            System.out.print(a[j][i]+" ");
        }
    }
    else{
        for(int j=a.length-1;j>=0;j--){
            System.out.print(a[j][i]+" ");

        }
    }
}

Thanks in advance 提前致谢

It seems what you want is to print each column of the matrix, where the even indexed columns are printed in ascending order and the odd indexed columns are printed in descending order. 看起来你想要的是打印矩阵的每一列,其中偶数索引列按升序打印,奇数索引列按降序打印。

for (int col = 0; col < a[0].length; col++) {
    if (col % 2 == 0) {
        for (int row = 0; row < a.length; row++)
            System.out.print(a[row][col] + " ");
        System.out.println();
    } else {
        for (int row = a.length - 1; row >= 0; row--)
            System.out.print(a[row][col] + " ");
        System.out.println();
    }
}

Output : 输出:

1 5 9 13 17 
18 14 10 6 2 
3 7 11 15 19 
20 16 12 8 4 

You can do it easily by taking two for loops, one for forward scanning and another for backward scanning. 您可以通过两个for循环轻松完成,一个用于正向扫描,另一个用于向后扫描。

Here is the code snippet: 这是代码片段:

public static void main (String[] args)
{
    int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};

    for(int k = 0;k < 4;k++) {
        for(int i = 0;i < a.length;i++) {
            System.out.print(a[i][k] + " ");
        }

        k++;
        for(int i = a.length - 1;i >=0 ;i--) {
            System.out.print(a[i][k] + " ");
        }
    }
}

Output: 输出:

1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4 

That's like traversing an array similiar to traversing a tree in zigzag traversal, but in this case we do not need stacks just modular arithmetich will be enough, here is your solution; 这就像遍历一个类似于在Z字形遍历中遍历树的数组,但在这种情况下我们不需要堆栈只需模块化arithmetich就足够了,这是你的解决方案;

Method #1 - dummyPrint 方法#1 - dummyPrint

private static void dummyPrint(int[][] array) {
    System.out.print("Dummy Print: ");

    for(int j = 0; j < array[0].length; j++) {
        if(j%2 == 0) {
            for(int i = 0; i < array.length; i++)
                System.out.printf("%2d ", array[i][j]);
        } else {
            for(int i = array.length-1; i >= 0; i--)
                System.out.printf("%2d ", array[i][j]);
        }
    }

    System.out.println();
}

Method #2 - prettyPrint 方法#2 - prettyPrint

private static void prettyPrint(int[][] array) {
    System.out.println("Pretty Print;");
    System.out.println("*************");
    for(int j = 0; j < array[0].length; j++) {
        if(j%2 == 0) {
            for(int i = 0; i < array.length; i++)
                System.out.printf("%2d ", array[i][j]);
        } else {
            for(int i = array.length-1; i >= 0; i--)
                System.out.printf("%2d ", array[i][j]);
        }


        System.out.println();
    }
}

Demonstration Code 示范代码

public class ArrayDemo {

    public static void main(String[] args) {
        int [][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};

        dummyPrint(array);

        System.out.println();

        prettyPrint(array);
    }

    private static void dummyPrint(int[][] array) {
        System.out.print("Dummy Print: ");

        for(int j = 0; j < array[0].length; j++) {
            if(j%2 == 0) {
                for(int i = 0; i < array.length; i++)
                    System.out.printf("%2d ", array[i][j]);
            } else {
                for(int i = array.length-1; i >= 0; i--)
                    System.out.printf("%2d ", array[i][j]);
            }
        }

        System.out.println();
    }

    private static void prettyPrint(int[][] array) {
        System.out.println("Pretty Print;");
        System.out.println("*************");
        for(int j = 0; j < array[0].length; j++) {
            if(j%2 == 0) {
                for(int i = 0; i < array.length; i++)
                    System.out.printf("%2d ", array[i][j]);
            } else {
                for(int i = array.length-1; i >= 0; i--)
                    System.out.printf("%2d ", array[i][j]);
            }


            System.out.println();
        }
    }
}

The Output 输出

Dummy Print:  1  5  9 10  6  2  3  7 11 12  8  4 

Pretty Print;
*************
 1  5  9 
10  6  2 
 3  7 11 
12  8  4 

There is a discrepancy with the code you print and the desired output. 与您打印的代码和所需的输出存在差异。 In your code, there is a line that prints "row" and another that prints "column". 在您的代码中,有一行打印“行”,另一行打印“列”。

This code 这段代码

public class Wave1 {   
  public static void main(String[] args) {
    int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
    String separator="";
    System.out.print("{");
    for (int[] row: a) {
      for (int cell: row) {
        System.out.print(separator+cell);
        separator=",";
      }
    }
    System.out.println("}");
  }
}

prints 版画

{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20} 

I was not able to figure out the order in which the numbers should be printed. 我无法弄清楚应该打印数字的顺序。 I assumed it did not matter. 我认为没关系。

public class Wave1 {

    public static void main(String args[])
    {
        int [][] wave={{1,2,3,4},{5,6,7,8},{8,9,10,11},{12,13,14,15}};
        for(int i=0;i<wave.length;i++){
            if(i%2==0){
            for(int j=0;j<wave[0].length;j++){
                System.out.print(wave[i][j]+" ");
                }
            System.out.println();
            }
            else{
                for(int j=wave[0].length-1;j>=0;j--){
                    System.out.print(wave[i][j]+" ");
                }
                System.out.println();
            }
        }
    }
}
public static void wave(int[][]input)
    {
        int row=0;
        int col=0;
        int steps=1;
        System.out.print(input[0][0]+" ");
        while(steps<=input.length*(input[0].length)-1)
        {
            if(col%2==1)
            {
                row--;

            }
            else
            {
                row++;

            }
            if(row<0)
            {
                row++;
                if(col%2==1)
                {
                    col++;
                }
            }
            if(row>input.length-1)
            {
                row--;
                if(col%2==0)
                {
                    col++;
                }

            }
            System.out.print(input[row][col]+" ");
            steps++;
        }

    }

Check this one: Use this function it will take 2D Array as its input and would print a zigzag Path 检查一下:使用此功能,它将以2D Array作为输入,并打印Z字形路径

 public static void wavePrint(int input[][]){

   int count = 1;
   int rows = input.length;
   int cols = input[0].length;

   for(int i = 0; i < cols; i++){
       if(count % 2 != 0){           //odd column
           int sine_e = 0;
           while(sine_e < rows){
               System.out.print(input[sine_e][i]+" ");
               sine_e++;
           }
           count++;
       }else{
           int sine_e = rows - 1;          //even column
           while(sine_e >= 0){
               System.out.print(input[sine_e][i]+" ");
               sine_e--;
            }
            count++;
       }
   }//end of for loop
}

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

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