【发布时间】:2015-03-26 16:24:07
【问题描述】:
您好,我正在尝试编写一个采用二维数组并像这样镜像它的代码,
input: and get the output like so:
123 321
456 654
789 987
我有这部分代码:
public static void mirror(Object[][] theArray) {
for(int i = 0; i < (theArray.length/2); i++) {
Object[] temp = theArray[i];
theArray[i] = theArray[theArray.length - i - 1];
theArray[theArray.length - i - 1] = temp;
}
}
}
我的程序可以运行,但是它返回的结果相反
input: and get the output like so:
123 789
456 456
789 123
我做错了什么..?
【问题讨论】:
-
您正在反转数组的第一个维度,而不是第二个维度。您需要将
for中的mirror循环包裹在另一个for 循环中,并适当调整索引。
标签: java arrays multidimensional-array mirror