为什么 `collections.reverse()` 并未为原始的 `int[]` 数组返回反转值?

huangapple go评论77阅读模式
英文:

Why collections.reverse() is not returning reverse value for primitive int[] array?

问题

考虑以下代码片段:

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 contained in 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 contained in the array now is : [boss, bob, cat, tom]

但是它没有反转 int[]。我真的不明白为什么。我参考了这个 Stack Overflow 的问题(https://stackoverflow.com/questions/50557566/collections-reverse-doesnt-work-correctly),但对于为什么基本类型没有被反转并填充回传递的原始 int[],它没有真正帮助我理解。

英文:

Consider the following snippet,

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));
	}

It's returning me the expected result for Integer[] and String[] as

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]

but it does not reverse the int[]. I didn't understand why really. I referred to this SO question (https://stackoverflow.com/questions/50557566/collections-reverse-doesnt-work-correctly) but it didn't help me really to understand why primitives are not reversed back and populated on original int[] passed.

答案1

得分: 3

Your Arrays.asList for the int[] array is not producing the result you think. There is no way to create a List<int> in Java so, you are simply creating a List<int[]>, i.e. a list of a single item, where that item is your original int[] array. If you reverse that single item, well, you get that item back, and that's why it appears to you as if nothing had happened when you print its reversed contents.

The confusion seems to come from the expectation that when you call List.asList(int[]) you should be getting back a boxed array of integers, i.e. List<Integer>, but that is not the case. There is no such thing as aggregated auto boxing in Java and as explained in the other answer, the compiler makes the assumption that what you want is to create a list of a single element for that int[] array.

英文:

Your Arrays.asList for the int[] array is not producing the result you think. There is no way to create an List&lt;int&gt; in Java so, you are simply creating a List&lt;int[]&gt;, i.e. a list of a single item, where that item is your original int[] array. If you reverse that single item, well, you get that item back, and that's why it appears to you as if nothing had happened when you print its reversed contents.

The confusion seems to come from the expectation that when you call List.asList(int[]) you should be getting back a boxed array of integers, i.e. List&lt;Integer&gt;, but that is not the case. There is no such thing as aggregated auto boxing in Java and as explained in the other answer, the compiler makes the assumption that what you want is to create a list of a single element for that int[] array.

答案2

得分: 1

Arrays.asList(arr) 返回一个 List<int[]>,其唯一元素是您的原始数组。当您反转仅包含一个元素的 List 时,结果仍然是相同的 List

这种行为的原因在于 &lt;T&gt; List&lt;T&gt; asList(T... a) 接受一个某种泛型类型参数 T 的数组,而且 T 必须是引用类型,不能是原始类型。因此,当您传递一个 int[] 时,T 是一个 int[](这是引用类型),而不是原始类型 int

对于 Integer[]String[],行为是不同的,因为 IntegerString 都是引用类型,所以 Arrays.asList() 会返回一个 List<Integer>(对于 Integer[])和一个 List<String>(对于 String[])。

英文:

Arrays.asList(arr) returns a List&lt;int[]&gt; whose only element is your original array. When you reverse a List of a single element, the result is the same List.

The reason of this behavior is that &lt;T&gt; List&lt;T&gt; asList(T... a) accepts an array of some generic type parameter T, and T must be a reference type. It cannot be a primitive. Therefore, when you pass an int[], T is an int[] (which is a reference type) and not the primitive int.

The behavior for Integer[] and String[] is different, because both Integer and String are reference types, so Arrays.asList() returns a List&lt;Integer&gt; for Integer[] and a List&lt;String&gt; for String[].

huangapple
  • 本文由 发表于 2020年7月26日 13:55:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63096592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定