如何以锯齿路径遍历和打印二维数组

3
假设我有一个数组
A[][] = {{ 1, 2, 3, 4},
         { 5, 6, 7, 8},
         { 9,10,11,12}};

我希望您能打印出一个波形,使输出结果如下:
{1,5,9,10,6,2,3,7,11,12,8,4}

我该怎么做呢?这是我的代码,但它给我返回了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]+" ");

        }
    }
}

感谢您的提前帮助。

你想让它每次生成随机顺序,还是按照你上面所述的顺序? - Josh Stevens
3
我不确定您在这里试图做什么,但是您之所以会得到“IndexOutOfBounds”的错误,是因为这行代码:i < a.length+1 - user2004685
我刚找到了更好的方法,请查看下面的答案,我的答案提供了dummyPrint(int[][] array)和prettyPrint(int[][] array)两种方法,希望能对你有所帮助。 - Levent Divilioglu
7个回答

3

看起来你想要打印矩阵的每一列,其中偶数索引的列按升序打印,奇数索引的列按降序打印。

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

输出:

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

1

通过使用两个for循环,一个用于正向扫描,另一个用于反向扫描,您可以轻松地完成它。

这是代码片段:

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

输出:

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

1
这就像在Zigzag遍历中遍历数组类似于遍历树,但在这种情况下,我们不需要堆栈,只需使用模算术即可,以下是您的解决方案;

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

方法二 - prettyPrint
(注:保留了HTML标记)
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();
    }
}

演示代码

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

输出

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 

0

您打印的代码与期望输出存在差异。在您的代码中,有一行打印了“row”,另一行打印了“column”。

此代码

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

打印

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

我无法弄清楚数字应该以什么顺序打印。我假设顺序并不重要。


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

0

看这个: 使用这个函数,它将以2D数组作为输入,并打印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
}

0
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++;
        }

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接