排序—在java中实现对象的arraylist的排序和搜索方法

ekqde3dh  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(396)

我知道基本的编程,但我对oop和java还不熟悉。我正在尝试缩短和搜索对象的数组列表。我知道有很多方法可以做到这一点,但我喜欢两种方法,java list.contains(字段值等于x的对象)和sort arraylist,当自定义对象在主类中时,它们按属性代码排序,但我想在student类中移动它们,并调用我自己的排序和搜索方法。sort函数仍然有效,但搜索返回错误的索引整数。我找不到我丢失的东西。完整代码如下。

/* implementation of binary search found at
https://stackoverflow.com/questions/18852059/
and sort solution at
https://stackoverflow.com/questions/2784514/
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class test {
    static ArrayList<Student> course = new ArrayList<>();

    public static void main(String[] args) {
        System.out.print("Enter name to search: ");
        Scanner input = new Scanner(System.in);
        String stName = input.nextLine();

        course.add(new Student("will", 353429, 13.2));
        course.add(new Student("joe", 353430, 12.1));
        course.add(new Student("bill", 353431, 14.9));
        course.add(new Student("jim", 353432, 15.3));
        course.add(new Student("jack", 353473, 11.2));
        course.add(new Student("jean", 353439, 16.8));
        course.add(new Student("jill", 353333, 14.9));
        course.add(new Student("jane", 353432, 15.7));
        course.add(new Student("john", 353421, 10.6));
        course.add(new Student("ben", 353438, 16.0));
        course.add(new Student("dave", 353529, 14.9));
        course.add(new Student("jim", 352989, 15.3));
        course.add(new Student("doug", 353178, 11.2));

        sortStudents();

/*  // search part works when its here , inside main class
        int idx = Collections.binarySearch(course, new Student( stName ), new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });

* /

        int idx = searchStudent( stName );

        System.out.println("Sorted list");
        for (int cnt=0;cnt<course.size();cnt++){
            System.out.println(cnt + " "+ course.get(cnt).toString());
        }

        if (idx>0){
            System.out.println(stName +" found on line: " + idx);
            }else{
            System.out.println(" Not in the list" +idx);
        }
    }

    static void sortStudents(){
        Collections.sort(course,Student.sortByName);
    }

    static int searchStudent(String nm){
        return Collections.binarySearch(course, new Student(nm));
    }

    static  class Student implements Comparable<Student>{
        private String name;
        private int age;
        private double points;

        public Student(String nm) {  this.name = nm; } // constructor

        public Student(String nm, int n, double p) {  // constructor
            this.name = nm;
            this.age = n;
            this.points = p;  }

        public String getName(){ return name; }
// but search code fails when moved here 
        public static Comparator<Student> binarySearch = new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        };

        public static Comparator<Student> sortByName = new Comparator<Student>() {
            @Override
            public int compare(Student obj1, Student obj2) {
                //sort in ascending order
                return obj1.name.compareTo(obj2.name);
                //sort in descending order
                //return obj2.name.compareTo(obj1.name);
            }
        };

        @Override
        public int compareTo(Student o) {
            return 0;
        }

        @Override
        public String toString() {
            return "Name:" + name + ", Age:" + age;
        }
    }
}
cpjpxq1n

cpjpxq1n1#

问题

那是因为你 compareTo ,因为您没有实现如何比较 Student 在他们之间,你只实现了一个名字 comparator ,这是 Collections.binarySearch ```
@Override
public int compareTo(Student o) {
return 0;
}

你可以用显式 `Comparator` ```
static int searchStudent(String nm) {
    return Collections.binarySearch(course, new Student(nm), Student.sortByName);
}

改善

Comparator<Student> binarySearch 与相同 Comparator<Student> sortByName ,没有理由让两件事做相同的事。
你也要明白这一点
你需要一个 Comparator 告诉如何排列对象
制作对象 Comparable 所以它知道如何和其他人一起点餐
在这里你做了两个,这是没有用的,删除 Comparator s定义

修复

所以只有一个 compareTo 方法就行了

@Override
public int compareTo(Student o) {
    int diff = this.name.compareTo(o.name);
    if (diff != 0) {
        return diff;
    }
    return Integer.compare(this.age, o.age);
}

它可以简化为 Comparator 这允许一些方法链接。请注意,它是 private ,它不应该在类外部手动使用,因为类现在自己知道如何对元素排序

static private Comparator<Student> studComp = Comparator.comparing(Student::getName)
                                                    .thenComparing(Student::getAge);

@Override
public int compareTo(Student o) {
    return studComp.compare(this, o);
}

在主课堂上给予

static void sortStudents() {
    course.sort(Comparator.naturalOrder());
}

static int searchStudent(String nm) {
    return Collections.binarySearch(course, new Student(nm));
}

相关问题