如何使用Object参数对Arraylist中的对象进行排序 [英] How to sort Objects in an Arraylist with an Object parameter

查看:103
本文介绍了如何使用Object参数对Arraylist中的对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了在Arraylist中排序对象的问题,因为我是排序对象的新手。



对一个arraylist进行排序是非常基本的,但是对对象的arraylist进行排序是完全不同的事情。基本上我一直在堆栈溢出代码,人们似乎使用比较器来解决他们的问题,但他们没有解释如何实际调用该方法并将其投入使用,这就是为什么我在这里。



使用下面的代码我尝试使用参数 - 字符串名称,int年龄和字符串课程对学生的arraylist进行排序。然后我有另一个类来存储Student对象并在类中对它们进行排序。以下是我的内容:



Student.java

  public class Student {

private String name,course;
private int age;

public Student(String name,int age,String course){
this.name = name;
this.age =年龄;
this.course = course;
}

public String getName(){
return name;
}

public int getAge(){
return age;
}

public String getCourse(){
return course;
}
}

CompareObj.java

  import java.util。*; 

公共类CompareObj实现Comparator< Student> {

private ArrayList< Student> SLIST;

public CompareObj(){
sList = new ArrayList< Student>();
}

@Override
public int compare(学生s1,学生s2){
返回s2.getName()。compareTo(s1.getName());
}

public void add(学生s1){
sList.add(s1);
}

public void displayList(){
for(int i = 0; i< sList.size(); i ++){
System.out.println (sList.get(ⅰ).getName());
}
}
}

在CompareObj类中,怎么能我使用实现的方法compare(s1,s2),如何使用这个方法对学生对象的arraylist进行排序?

解决方案

 我如何使用此方法对学生对象的arraylist进行排序? 

您无需致电 compare()你自己。您可以将比较器传递给 Collections.sort(),通过调用 compare()来为您排序使用自定义类 CompareObj

  Collections.sort(studentList,new CompareObj()); 

或者没有 CompareObj 的另一种方式是,

  Collections.sort(studentList,new Comparator< Student>(){
@Override
public int compare (学生s1,学生s2){
返回s1.getName()。compareToIgnoreCase(s2.getName());
}
});


I am having problems with sorting objects in an Arraylist as I am new to sorting object.

Sorting an arraylist is pretty basic, but sorting an arraylist of objects is a completely different matter. Basically I have been going over code here in stack overflow and people seem to use comparators to solve their problems, but they don't explain how to actually call the method and put it into use, and that is why I am here.

With the code below I am trying to sort an arraylist of Students with parameters - String name, int age and string course. I then have another class to store Student objects and sort them within the class. Here is what I have :

Student.java

public class Student {

    private String name, course;
    private int age;

    public Student(String name, int age, String course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getCourse() {
        return course;
    }
}

CompareObj.java

import java.util.*;

public class CompareObj implements Comparator<Student>{

    private ArrayList<Student> sList;

    public CompareObj() {
        sList = new ArrayList<Student>();
    }

    @Override
    public int compare(Student s1, Student s2) {
         return s2.getName().compareTo(s1.getName());
    }

    public void add(Student s1) {
        sList.add(s1);
    }

    public void displayList() {
        for(int i = 0; i<sList.size();i++) {
            System.out.println(sList.get(i).getName());
        }
    }
}

In CompareObj class, how can I use the implemented method compare(s1,s2), how can I use this method to sort my arraylist of student objects?

解决方案

how can i use this method to sort my arraylist of student objects?

You don't need to call compare() yourself. You can just pass your comparator to Collections.sort() that will take care sorting for you by calling compare() method.


By using custom class CompareObj,

Collections.sort(studentList, new CompareObj());

Or another way without CompareObj is,

Collections.sort(studentList,new Comparator<Student>() {
         @Override
        public int compare(Student s1, Student s2) {
                return s1.getName().compareToIgnoreCase(s2.getName());
        }
    });

这篇关于如何使用Object参数对Arraylist中的对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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