class LowHighSwapstatic void doIt int z int temp =zzlength-1;zzlength-1=z;zo = temp;class TestItpublic static void main String args int myArray = 1 2 345;LowHighSwapdoItmyArray;for int i =0; i myArray
这段代码存在一些错误,需要进行修正才能正常运行。修正后的代码如下:
class LowHighSwap {
static void doIt(int[] z) {
int temp = z[z.length - 1];
z[z.length - 1] = z[0];
z[0] = temp;
}
}
class TestIt {
public static void main(String[] args) {
int[] myArray = {1, 2, 3, 4, 5};
LowHighSwap.doIt(myArray);
for (int i = 0; i < myArray.length; i++) {
System.out.print(myArray[i] + "");
}
}
}
修正后的代码会输出:5 2 3 4 1
原因是在doIt方法中,将数组最后一个元素与第一个元素进行了交换,所以输出的数组为:5 2 3 4 1
原文地址: https://www.cveoy.top/t/topic/i0ES 著作权归作者所有。请勿转载和采集!