Android:ArrayList 将项目移动到位置 0 [英] Android: ArrayList Move Item to Position 0
问题描述
我有一个 ArrayList,我需要确保特定项目位于 0 位置,如果不是,我需要将其移动到那里.该项目上有一个 isStartItem
布尔值,因此我可以轻松找到需要位于位置 0 的特定项目,但是如何将其移动到正确的位置?
I have an ArrayList and I need to make sure a specific item is at the 0 position and if it is not, I need to move it there. The item has an isStartItem
boolean on it, so I can easily find the specific item I need to be in position 0 but then how do I go about moving it to the right position?
我假设我需要使用这样的东西:
I am assuming I need to use something like this:
for(int i=0; i<myArray.size(); i++){
if(myArray.get(i).isStartItem()){
Collection.swap(myArray, i, 0);
}
}
但这似乎不起作用...
But this does not seem to work...
推荐答案
你需要使用 Collections
类的 swap
方法.Collections
,以 s
结尾.
You need to use Collections
class's swap
method. Collections
, with an s
at the end.
改变 -
Collection.swap(myArray, i, 0);
到这个 -
Collections.swap(myArray, i, 0);
看看这个示例.
集合和
Collection and Collections are two different things in Java. The first one is an interface, the second one is a class. The later one has a static swap method, but the former one doesn't.
这篇关于Android:ArrayList 将项目移动到位置 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!