2017-11-18 46 views
0

例如,我想打印四分之一或二維數組的某個部分,我將如何更改下面的代碼。只是一般只打印一樣,如果有十行和列的某些部分我會如何打印最後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(); 
    } 
+0

第一個問題:什麼季度? – tadman

+0

只是一般印刷只有某些部分,如有10行和colums如何,我會打印最後2行和最後2 colums –

+0

@ hephaestus哈迪斯可以用你的2D陣列更新帖子。這將有助於給出答案。 – Dev4World

回答

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

這將打印一半的行和列。

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

可以說我想得到........................................... ......................................... b | c | d | .................................................. .................................................. ............... f | g | h | 。我怎麼做 –

+0

或者只是打印| d | .................................................. .................................................. .................................................. ........ | H | ....................................... .................................................. .............................................. –

+0

@hephaestushades檢查爲更新的答案.. –

0

您可以使用掃描儀將採取定製ROW和COL值。當然,您需要檢查提供的值是否在打印之前的長度中,以免發生任何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();