Java Collections.rotate() 与数组不起作用
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个棘手的问题:是的,
asList
支持它随数组返回的List
,并且对List
的更改将“直写”到数组。但是,由于在这种情况下T...
的 varargs 与原始类型数组交互的方式,您实际上正在创建一个包含 1 个元素的列表!让我们尝试一些不同的东西:
如您所见,带有
int[]
的可变参数不能按您预期的方式工作,并且编译器会给出错误。Arrays.asList
实际上返回 1 元素List
,而不是 5 元素List
。使用
Integer[]
而不是int[]
按预期工作:更多说明
asList
的完整签名是;列表Arrays.asList(T... a)
。请注意,在这种情况下,T
不能是int
,这与 Java 中不能有List
的原因相同:T
需要是引用类型。考虑以下代码片段:
这里发生的情况是,每个
int
被装箱到一个Integer
中,并且 varargs 机制“起作用”,并且asList
创建一个3 个元素的列表。现在考虑以下形式:现在
asList
的参数是int[]
。T
不能是int
,因此,T...
varargs 机制“失败”,并且asList
> 只获取一个元素,并且它是一个int[]
,而不是int
值本身。现在考虑这种形式:
现在,由于
Integer[]
是一个T...
,asList
按预期获取 3 个元素。另请参阅
int[]
不会自动装箱为Integer[]
This is a tricky problem: yes,
asList
backs theList
it returns with the array, and changes to theList
will "write-through" to the array. However, due to how varargs ofT...
interacts with an array of primitive type in this case, you're actually creating a list with 1 element!Let's try something different:
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-elementList<int[]>
instead of a 5-elementList<Integer>
.Using
Integer[]
instead ofint[]
works as expected:More explanation
The full signature of
asList
is<T> List<T> Arrays.asList(T... a)
. Note thatT
can't beint
in this case, for the same reason why you can't have aList<int>
in Java:T
needs to be a reference type.Consider the following snippet:
What happens here is that each
int
is boxed into anInteger
, and the varargs mechanism "works" andasList
creates a list of 3 elements. Now consider the following form instead:Now the argument to
asList
is anint[]
.T
can't be anint
, therefore, theT...
varargs mechanism "fails", andasList
only gets one element, and it's anint[]
, instead of theint
values themselves.Now consider this form:
Now since
Integer[]
is aT...
,asList
gets 3 elements as expected.See also
int[]
does not autobox to anInteger[]