【发布时间】:2015-06-30 03:45:12
【问题描述】:
所以我正在编写一个函数,它应该交换数组的第一个和最后一个元素并返回修改后的数组。我的代码如下:
public static int[] swapEnds(int[] nums) {
int newArray[] = new int[nums.length];
newArray = nums; // copies all the elements to the new array
newArray[0] = nums[nums.length -1 ]; // changes the first element of the newArray
newArray[newArray.length-1] = nums[0]; // changes the last element of the newArray
return newArray;
}
通过一些调试,我发现 nums[0] 已经以某种方式更改,但我没有在我的代码中的任何地方进行更改。任何帮助将非常感激。谢谢。
【问题讨论】: