Android:ArrayList 将项目移动到位置 0 [英] Android: ArrayList Move Item to Position 0

查看:32
本文介绍了Android:ArrayList 将项目移动到位置 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);

看看这个示例.

集合集合 在 Java 中是两种不同的东西.第一个是接口,第二个是类.后一个有静态交换方法,但前一个没有.

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屋!

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