Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据id号将学生插入列表_Java_For Loop_Linked List - Fatal编程技术网

Java 根据id号将学生插入列表

Java 根据id号将学生插入列表,java,for-loop,linked-list,Java,For Loop,Linked List,所以我正在研究一种方法,我有点困惑,重点是将一个学生放入一个基于id号的列表中,所以id号较低的学生排在第一位,等等。。。一旦我检查if station是否更大,我就不知道该在if station内做什么 我现在的代码是 public boolean insort(StudentIF s) { StudentLLNode curr = head; if (s == null) { return false; } if (head == null)

所以我正在研究一种方法,我有点困惑,重点是将一个学生放入一个基于id号的列表中,所以id号较低的学生排在第一位,等等。。。一旦我检查if station是否更大,我就不知道该在if station内做什么

我现在的代码是

public boolean insort(StudentIF s) {
    StudentLLNode curr = head;

    if (s == null) {
        return false;
    }
    if (head == null) {
        StudentLLNode student = new StudentLLNode(s);
        head = student;
        size++;
        //System.out.println("working");
        return true;
    } else {
        if (curr.getStd().compareTo(s) == 0) {
            //System.out.println("working");
            return false;
        }
        while (curr.getNext() != null) {
            if(s.compareTo(curr.getStd()) == 1){
                //confused here 
            }
            curr = curr.getNext();
        }
        //confused here
        StudentLLNode student1 = new StudentLLNode(s);
        curr.setNext(student1);
        size++;
        return true;
    }
}
我的比较方法是

public int compareTo(StudentIF other) {
    if (other == null) {
        return 1;
    } // satisfies null student
    if (this.id > other.getId())
        return 1;
    else if (this.id < other.getId())
        return -1;
    else
        return 0; // if it's neither smaller nor larger, it must be equal
}
二进制搜索返回studentObject的索引,如果该对象不在列表中,则返回-insertIndex-1

int index = Collections.binarySearch(myStudentList, studentObject);
myStudentList.add(index < 0 ? -1 * (index + 1) : index, studentObject);
/* OR
if(index < 0)
    myStudentList.add(-1 * (index + 1), studentObject);
If you did not want to add duplicates
*/