简体   繁体   English

我该如何更改我的数组列表?

[英]How can i alter my arraylist?

i search more about my problem, but know i thought about it and i need to alter the arraylist. 我搜索有关我的问题的更多信息,但知道我对此有所考虑,因此需要更改arraylist。 I saw about this fuctions Collections.swap(list,x,y), and this is cool, but only if i like to change one element for other. 我看到了有关此功能的Collections.swap(list,x,y),这很酷,但前提是我想将一个元素更改为另一个。 So i need a method that change the first places and put my users online with this condition: 因此,我需要一种可以更改首位并使这种情况下的用户在线的方法:

if(userpresence.getmode()==null&&userpresence.isAvailable()){
listItem.add(new contacts(name,user,icon))
}
if(userpresence.getmode()==null&&!userpresence.isAvailable()){
    listItem.add(new contacts(name,user,icon))
}

in this part i show the list sort aphabetically: 在这一部分中,我按字母顺序显示列表:

    Collections.sort(listItem);

//I think the method about I tell you is in this part 

    ObservableList<Contacts> ob=FXCollections.observableArrayList(listItem);
    listview.setitems(ob);

and the idea is something like this: 这个想法是这样的:

user online
user online
user online
user online
user offline
user offline
user offline
user offline
user offline

but in this case only i show in this way: 但是在这种情况下,只有我以这种方式显示:

user offline
user offline
user offline
user offline
user offline
user online
user offline
user offline
user offline
user online
user offline
user offline
user offline
user online
user offline
user offline
user offline
user online
user offline
user offline
user offline
user online
user offline
user offline
user offline
user offline
user online
user offline
user offline

because I try to do the comparasing and the effect about this not shown. 因为我尝试进行比较,但未显示此效果。 this is my method comparator: 这是我的方法比较器:

public static Comparator<Contacts> contactsComparator=new Comparator<Contacts>(){
    @Override
    public int compare(Contacts c1, Contacts c2) {
        String ContactName1=c1.getName().toUpperCase();
        String ContactName2=c2.getName().toUpperCase();


        return ContactName1.compareTo(ContactName2);
    }
};

and in the collections.sort i put this (arraylist,contacts.contactsComparator) 并且在collections.sort中,我把它(arraylist,contacts.contactsComparator)

Collections.sort(list) only works if your objects implement the interface java.lang.Comparable . 仅当您的对象实现接口java.lang.Comparable时, Collections.sort(list)才有效。 Otherwise you need to implement and pass a java.util.Comparator to the sort method as well. 否则,您还需要实现并将java.util.Comparator传递给sort方法。 Either is needed to define the sort order for your objects. 都需要定义对象的排序顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM