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

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

问题描述

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

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

使用下面的代码,我尝试使用参数对学生数组列表进行排序 - 字符串名称、整数年龄和字符串课程.然后我有另一个类来存储 Student 对象并在类中对它们进行排序.这是我所拥有的:

Student.java

公开课学生{私人字符串名称,当然;私人整数年龄;公共学生(字符串名称,整数年龄,字符串课程){this.name = 名称;this.age = 年龄;this.course = 课程;}公共字符串 getName() {返回名称;}公共 int getAge() {回归年龄;}公共字符串 getCourse() {回程;}}

CompareObj.java

import java.util.*;公共类 CompareObj 实现了 Comparator{私有 ArrayList列表;公共比较对象(){sList = new ArrayList();}@覆盖公共 int 比较(学生 s1,学生 s2){返回 s2.getName().compareTo(s1.getName());}公共无效添加(学生 s1){sList.add(s1);}公共无效显示列表(){for(int i = 0; i<sList.size();i++) {System.out.println(sList.get(i).getName());}}}

在CompareObj 类中,如何使用实现的方法compare(s1,s2),如何使用该方法对我的学生对象数组列表进行排序?

解决方案

如何使用此方法对我的学生对象数组列表进行排序?

您不需要自己调用 compare().您可以将比较器传递给 Collections.sort(),它会通过调用 compare() 方法为您进行排序.

<小时>

通过使用自定义类CompareObj,

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

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

Collections.sort(studentList,new Comparator() {@覆盖公共 int 比较(学生 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天全站免登陆