如何打印2d阵列的一部分

时间:2017-11-18 05:34:10

标签: java arrays multidimensional-array

比如说我想要打印半个四分之一或2D阵列的某个部分,我将如何更改下面的代码。一般情况下,只打印某些部分,比如有10行和列,我将如何打印最后2行和最后2列,

for (int row = 0; row < example.length; row++) {
for (int column = 0; column < example[row].length; column++) {    
    System.out.print(" "+example[row][column] + "\t|");  
    }
  System.out.println(); 
  }

3 个答案:

答案 0 :(得分:0)

您应该根据自己的意愿更改循环计数以进行打印。比如说

for (int row = 0; row < (example.length)/2; row++)
{
    for (int column = 0; column < (example.length)/2; column++) 
    {    
        System.out.print(" "+example[row][column] + "\t|");  
    }
    System.out.println(); 
}

这将打印一半的行和列。

答案 1 :(得分:0)

您可以尝试使用循环播放:

String[][] example = new String[][] { { "a", "b", "c", "d" }, { "e", "f", "g", "h" } };

for (int row = 0; row < example.length; row++) {
    for (int column = 0; column < example[row].length / 2; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");
for (int row = 0; row < example.length / 2; row++) {
    for (int column = 0; column < example[row].length; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");
for (int row = example.length / 2; row < example.length; row++) {
    for (int column = 0; column < example[row].length; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");
for (int row = 0; row < example.length; row++) {
    for (int column = example[row].length / 2; column < example[row].length; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");
for (int row = 0; row < example.length; row++) {
    for (int column = example[row].length / 4; column < example[row].length; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");
for (int row = 0; row < example.length; row++) {
    for (int column = example[row].length * 3 / 4; column < example[row].length; column++) {
        System.out.print(" " + example[row][column] + "\t|");
    }
    System.out.println();
}
System.out.println("----------------------------------------");

输出:

 a  | b |
 e  | f |
----------------------------------------
 a  | b | c | d |
----------------------------------------
 e  | f | g | h |
----------------------------------------
 c  | d |
 g  | h |
----------------------------------------
 b  | c | d |
 f  | g | h |
----------------------------------------
 d  |
 h  |
----------------------------------------

答案 2 :(得分:0)

您可以使用扫描程序获取自定义行和列值。当然,您需要检查提供的值是否在打印之前的每个长度中都是如此,这样您就不会得到任何IndexOutOfBounds错误(这里没有图示)。

int[][] example = new int[10][10];

    int count = 0;

//populate array

    for (int row = 0; row < example.length; row++) {
        for (int column = 0; column < example[row].length; column++) {    
            example[row][column] = count;
            count++;
        }
    }

    Scanner sc = new Scanner(System.in);
    System.out.println("Enter row start: ");
    int rowStart = sc.nextInt();

    System.out.println("Enter row end: ");
    int rowEnd = sc.nextInt();

    System.out.println("Enter col start: ");
    int colStart = sc.nextInt();

    System.out.println("Enter col end: ");
    int colEnd = sc.nextInt();

    for (int row = rowStart; row < rowEnd; row++) {
        for (int column = colStart; column < colEnd; column++) {    
            System.out.print(" "+example[row][column] + "\t|");  
        }
        System.out.println(); 
    }
    sc.close();
相关问题