Java的Collections.rotate()与数组不起作用 [英] Java Collections.rotate() with an array doesn't work

查看:229
本文介绍了Java的Collections.rotate()与数组不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Java code:

I have the following Java code:

import java.util.Arrays;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        int[] test = {1,2,3,4,5};
        Collections.rotate(Arrays.asList(test), -1);
        for(int i = 0; i < test.length; i++) { System.out.println(test[i]); }
    }

}

欲阵列被旋转,但是输出我得到的是

I want the array to be rotated, but the output I get is

1
2
3
4
5

这是为什么?

和是否有其他解决方案?

And is there an alternative solution?

编辑:

所以这个作品:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        int[] test = {1,2,3,4,5};
        List<Integer> testList = new ArrayList<Integer>();
        for(int i = 0; i < test.length; i++) { testList.add(test[i]); }
        Collections.rotate(testList, -1);
        for(int i = 0; i < test.length; i++) { System.out.println(testList.get(i)); }
    }

}

但Arrays.asList应该返回,当写入,复制更改数组列表。有什么办法来解决,而无需手动操作,从数组转换列出?

But Arrays.asList is supposed to return a list that when written to, copies the changes to the array. Is there any way to fix this without manually doing the conversion from array to list?

我(想我)不能浪费那么多的CPU时间和内存进行转换。

I (think that I) can't afford to waste that much CPU time and memory to do the conversion.

推荐答案

这是一个棘手的问题:是的, asList 备份的列表它与数组形式返回,并更改列表将通写到数组。但是,由于 T ... 如何可变参数与原始类型在这种情况下,一个数组进行交互,你实际上有1个元素创建列表!

This is a tricky problem: yes, asList backs the List it returns with the array, and changes to the List will "write-through" to the array. However, due to how varargs of T... interacts with an array of primitive type in this case, you're actually creating a list with 1 element!

    int[] test = {1,2,3,4,5};
    System.out.println(Arrays.asList(test).size());
    // prints "1"

让我们尝试不同的东西:

Let's try something different:

    int[] test = {1,2,3,4,5};
    List<Integer> list = Arrays.asList(test);
    // "Type mismatch: cannot convert from List<int[]> to List<Integer>"

正如你所见,你打算一个 INT [] 不工作方式的可变参数,编译器提供了一个错误。 Arrays.asList 实际上返回一个1元列表&LT; INT []&GT; ,而不是5元列表与LT;整数方式&gt;

As you see, the varargs with an int[] doesn't work the way you intended, and the compiler gives an error. Arrays.asList actually returns a 1-element List<int[]> instead of a 5-element List<Integer>.

使用整数[] 而不是 INT [] 按预期工作:

Using Integer[] instead of int[] works as expected:

    Integer[] test = {1,2,3,4,5};
    Collections.rotate(Arrays.asList(test), -1);
    System.out.println(Arrays.toString(test));
    // prints "[2, 3, 4, 5, 1]"


更多解释

的完整签名 asList 是的 &LT; T&GT;清单&LT; T&GT; Arrays.asList(T ...一) 。需要注意的是 T 不能 INT 在这种情况下,出于同样的原因,你不能有一个列表与LT; INT&GT; 在Java中: T 必须是引用类型


More explanation

The full signature of asList is <T> List<T> Arrays.asList(T... a). Note that T can't be int in this case, for the same reason why you can't have a List<int> in Java: T needs to be a reference type.

考虑下面的代码片段:

    System.out.println(Arrays.asList(1,2,3));
    // prints "[1, 2, 3]"

这里发生的是,每个 INT 是盒装入整数和可变参数机制作品和 asList 创建了3个元素的列表。现在考虑下面的形式:

What happens here is that each int is boxed into an Integer, and the varargs mechanism "works" and asList creates a list of 3 elements. Now consider the following form instead:

    System.out.println(Arrays.asList(new int[] { 1,2,3 }));
    // prints "[[I@xxxxxx]"

现在的参数 asList INT [] T 不能是 INT ,因此, T ... 可变参数机制失败,而 asList 只得到一个元素,它是一个 INT [] ,而不是 INT 值本身。

Now the argument to asList is an int[]. T can't be an int, therefore, the T... varargs mechanism "fails", and asList only gets one element, and it's an int[], instead of the int values themselves.

现在考虑这种形式:

    System.out.println(Arrays.asList(new Integer[] { 1,2,3 }));
    // prints "[1, 2, 3]"

现在因为整数[] T ... asList 获取3元素的预期。

Now since Integer[] is a T..., asList gets 3 elements as expected.


  • Java:基本数据类型的数组没有autobox

    • INT [] 不autobox到整数[]

    • Java: Array of primitive data types does not autobox
      • An int[] does not autobox to an Integer[]

      这篇关于Java的Collections.rotate()与数组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆