Java Collections.rotate() 与数组不起作用

发布于 2024-08-31 23:44:01 字数 1079 浏览 2 评论 0原文

我有以下 Java 代码:

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

}

我想要旋转数组,但我得到的输出是

1
2
3
4
5

这是为什么?

还有其他解决方案吗?

编辑:

所以这有效:

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 应该返回一个列表,当写入该列表时,会将更改复制到数组。有没有办法解决这个问题,而无需手动进行从数组到列表的转换?

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

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

Why is this?

And is there an alternative solution?

EDIT:

So this works:

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

}

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖果控 2024-09-07 23:44:01

这是一个棘手的问题:是的,asList 支持它随数组返回的 List,并且对 List 的更改将“直写”到数组。但是,由于在这种情况下 T... 的 varargs 与原始类型数组交互的方式,您实际上正在创建一个包含 1 个元素的列表!

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

让我们尝试一些不同的东西:

    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 元素 List,而不是 5 元素 List

使用 Integer[] 而不是 int[] 按预期工作:

    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 的完整签名是 ;列表Arrays.asList(T... a)。请注意,在这种情况下,T 不能是 int,这与 Java 中不能有 List 的原因相同: T 需要是引用类型。

考虑以下代码片段:

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

这里发生的情况是,每个 int 被装箱到一个 Integer 中,并且 varargs 机制“起作用”,并且 asList 创建一个3 个元素的列表。现在考虑以下形式:

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

现在 asList 的参数是 int[]T 不能是 int,因此,T... varargs 机制“失败”,并且 asList > 只获取一个元素,并且它是一个 int[],而不是 int 值本身。

现在考虑这种形式:

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

现在,由于 Integer[] 是一个 T...asList 按预期获取 3 个元素。

另请参阅

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>"

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>.

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]"

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.

Consider the following snippet:

    System.out.println(Arrays.asList(1,2,3));
    // prints "[1, 2, 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]"

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.

Now consider this form:

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

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

See also

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文