public class Test {

    static String[][] arr = new String[3][];

    private static void doPrint() {
        //insert code here
    }

    public static void main(String[] args) {
        String[] class1 = {"A", "B", "C"};
        String[] class2 = {"L", "M", "N", "O"};
        String[] class3 = {"I", "J"};
        arr[0] = class1;
        arr[1] = class2;
        arr[2] = class3;
        Test.doPrint();
    }
}

Which code fragment, when inserted at line //insert code here, enables the code to print COJ?



A.

int i = 0;
for (String[] sub : arr) {
    int j = sub.length - 1;
    for (String str : sub) {
        System.out.println(str[j]);
        i++;
    }
}
B.
for (int i = 0; i < arr.length; i++) {
    int j = arr[i].length - 1;
    System.out.print(arr[i][j]);
}
C.
int i = 0;
for (String[] sub : arr[][]) {
    int j = sub.length;
    System.out.print(arr[i][j]);
    i++;
}
D.
for (int i = 0; i < arr.length - 1; i++) {
    int j = arr[i].length - 1;
    System.out.print(arr[i][j]);
    i++;
}

題解

選項A,第10行會編譯錯誤,因為str變數的型態不是陣列。

選項B,輸出二維陣列每個一維陣列元素的最後一個元素,正好是「COJ」。

選項C,第7行會編譯錯誤,錯誤的foreach用法。

選項D,只會輸出「C」。