考虑以下代码段,
public static void main(String[] args) {
int[] arr = new int[] {1,2,3,4,5,6,7};
Collections.reverse(Arrays.asList(arr));
System.out.println("The value contained in the array is : " + Arrays.toString(arr));
Integer[] arr1 = new Integer[] {1,2,3,4,5,6,7};
Collections.reverse(Arrays.asList(arr1));
System.out.println("The value contained in the array is : " + Arrays.toString(arr1));
String[] strings = new String[] {"tom", "cat", "bob", "boss"};
Collections.reverse(Arrays.asList(strings));
System.out.println("The value contianed the the array now is : " + Arrays.toString(strings));
}
它为我返回了预期Integer[]的String[]结果
The value contained in the array is : [1, 2, 3, 4, 5, 6, 7]
The value contained in the array is : [7, 6, 5, 4, 3, 2, 1]
The value contianed the the array now is : [boss, bob, cat, tom]
但它并没有逆转int[]. 我真的不明白为什么。我提到了这个 SO 问题(Collections.reverse 不能正常工作),但它并没有帮助我真正理解为什么原语没有被反转回来并填充原始 int[] 传递。