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

时间:2016-03-13 14:26:45

标签: java arrays

假设我有一个数组

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

        }
    }
}

提前致谢

7 个答案:

答案 0 :(得分: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 :(得分: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 

答案 2 :(得分:1)

这就像遍历一个类似于在Z字形遍历中遍历树的数组,但在这种情况下我们不需要堆栈只需模块化的arithmetich就足够了,这是你的解决方案;

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

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

演示代码

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 

答案 3 :(得分:0)

您打印的代码与所需的输出存在差异。在你的代码中,有一行打印&#34; row&#34;另一个打印&#34;列&#34;。

此代码

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} 

我无法弄清楚应该打印数字的顺序。我认为没关系。

答案 4 :(得分: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();
            }
        }
    }
}

答案 5 :(得分: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++;
        }

    }

答案 6 :(得分:0)

检查一下: 使用此功能,它将以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
}
相关问题