安卓:ArrayList的将项目位置0 [英] Android: ArrayList Move Item to Position 0

查看:252
本文介绍了安卓: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 a 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...

推荐答案

您需要使用集合类的<​​code>交换方法。 集合,与取值结尾。

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

看看<一href="http://www.java2s.com/$c$c/Java/Collections-Data-Structure/SwapelementsofJavaArrayList.htm">this例如,

收集和的Collections 在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.

这篇关于安卓:ArrayList的将项目位置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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