首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数组中的比较差对象

数组中的比较差对象
EN

Stack Overflow用户
提问于 2017-08-12 08:18:14
回答 1查看 64关注 0票数 0

需要help.Have创建数组列表,然后添加多个对象(在我的例子中,在数组列表中添加2种类型的对象),.Then对数组列表.Able中的每个对象进行排序,但是我认为我的编码过于long.Is --它们的任何方式或方法都可以使其变得简单,而effective.Hope --你们都可以help.That,希望你们能够help.Thank。

代码语言:javascript
复制
public class Sample {


    static ArrayList<Object> object = new ArrayList<>();

    public static void main(String[] args){


        //initialize object
        User user1 = new User("user1","1");
        User user2 = new User("user1","3");
        Staff staf1 = new Staff("staf1","2");
        Staff staf2 = new Staff("staf2","4");

        //add object to arraylist
        object.add(user1);
        object.add(user2);
        object.add(staf1);
        object.add(staf2);


        //sort
        Collections.sort(object,new Comparator<Object>() {
            Object one,two;

            @Override
            public int compare(Object o1, Object o2) {

                User user1,user2;
                Staff staf1,staf2;

                ///identify ol class
                if(o1 instanceof User){
                   user1 = (User) o1;
                   one = (Object) user1;
                }else if(o1 instanceof Staff){
                    staf1 = (Staff) o1;
                    one = (Object) staf1;
                }

                //identify o2 class
                if(o2 instanceof User){
                     user2 = (User) o2;
                     two = (Object) user2;

                }else if(o2 instanceof Staff){
                    staf2 = (Staff) o2;
                    two = (Object) staf2;
                }

                //identify each object class
                //then compare value
                if(one instanceof User && two instanceof User){
                    //execute process
                    User userOne = (User) one;
                    User userTwo = (User) two;
                    return userOne.getRegister().compareTo(userTwo.getRegister());

                }else if(one instanceof Staff && two instanceof Staff){
                     //execute process
                    Staff stafOne = (Staff) one;
                    Staff stafTwo = (Staff) two;
                    return stafOne.getRegister().compareTo(stafTwo.getRegister());

                }else if(one instanceof User && two instanceof Staff){
                     //execute process 
                    User userOne = (User) one;
                    Staff stafTwo = (Staff) two;
                     return userOne.getRegister().compareTo(stafTwo.getRegister());
                }else if(one instanceof Staff && two instanceof User){
                     //execute process
                    Staff stafOne = (Staff) one;
                    User userTwo = (User) two;
                    return  stafOne.getRegister().compareTo(userTwo.getRegister());
                }else{
                    return 0;
                }
            }

        });

        //display item
        for(Object object : object){   
           if(object instanceof User){
               User user = (User)object;
               //display data
               System.out.println(user.getRegister());
           }else if(object instanceof Staff){
               Staff staf = (Staff) object;
               //display data
               System.out.println(staf.getRegister());
           }
        }


    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-12 08:33:08

由于您的对象具有相同的字段和方法,因此我建议您在这种情况下使用inheritance。首先,您必须排除一个抽象类,它是您的两个类的父类:

代码语言:javascript
复制
public abstract class Parent implements Comparable<Parent> { 

    abstract Integer getRegister();

    @Override
    public int compareTo(final Parent parent) {
        return this.getRegister().compareTo(parent.getRegister());
    }

}

然后,您必须使类UserStaff扩展Person类。然后,您可以创建list:List<Parent> list,并通过自己的比较器对其进行排序。因此,假设有matchinbg构造函数可用,User extends ParentStaff extends Parent

代码语言:javascript
复制
final List<Parent> list = new ArrayList<>();
list.add(new User("user1","1"));
list.add(new User("user1","3"));
list.add(new Staff("staf1","2"));
list.add(new Staff("staf2","4"));
Collections.sort(list);

编辑(因为OP不想使用继承):

而不是检查几次,只做一次,得到注册字段在你知道类型。

代码语言:javascript
复制
@Override
public int compare(Object o1, Object o2) {
    Integer register1 = null;
    Integer register2 = null;

    if(o1 instanceof User) {
        register1 = ((User)o1).getRegister();
    } else if(o1 instanceof Staff) {
        register1 = ((Staff)o1).getRegister();
    }

    if(o2 instanceof User) {
        register2 = ((User)o2).getRegister();
    } else if(o1 instanceof Staff) {
        register2 = ((Staff)o2).getRegister();
    }

    if(register1 != null && register2 != null) {
        return register1.compareTo(register2);
    }
    return 0;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45648102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档