20165303 Wei Yu made up for the tenth week of class

  • In data structures and algorithms, sorting is a very important operation. To make a class can be sorted, there are two ways:
  • There is a source code of a class, sort a member variable, let the class implement the Comparable interface, and call Collection.sort(List)
  • If there is no source code of the class, or multiple sorting, create a new class, implement the Comparator interface and call Collection.sort(List, Compatator)

For the following Student class, use Comparator programming to complete the following functions:

  1. Create a new student list in the test class StudentTest, including two students before and after oneself and student number, a total of 5 students, and give the running results (before sorting, after sorting)
  2. Sort the five students in increasing order by student number and total score, and submit two Comparator codes
    import junit.framework.TestCase;
    import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util. *;

public class TestStudent extends TestCase {
public static void main(String[] args) {
List

}
两个代码
import java.util.Comparator;
public class IDComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (Integer.parseInt(st1.getId())-Integer.parseInt(st2.getId()));
}
}
import java.util.Comparator;
public class scoreComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Student st1 = (Student)o1;
Student st2 = (Student)o2;
return (int) (st1.getTotalScore()-st2.getTotalScore());
}
}

  • See the attachment, supplement the content of MyList.java, submit a screenshot of the running result (full screen)
    , and push the code to the code cloud after class

import java.util.Iterator;

public class MyList {
public static void main(String[] args) {

    Node<Integer> S1 = new Node<Integer>(20165301, null);
    Node<Integer> S2 = new Node<Integer>(20165302, null);
    Node<Integer> S3 = new Node<Integer>(20165304, null);
    Node<Integer> S4 = new Node<Integer>(20165305, null);
    //把上面四个节点连成一个没有头结点的单链表
    S1.next = S2;
    S2.next = S3;
    S3.next = S4;
    //遍历单链表,打印每个结点的
    Node<Integer> s = S1;
    while (s != null) {
        System.out.println(s.data);
        s = s.next;
    }
    System.out.println();
    //把你自己插入到合适的位置(学号升序)
    Node<Integer> M = new Node<Integer>(20165303, null);
    s = S1;
    while (s != null) {
        if (s.data < 20165303 && s.next.data > 20165303) {
            M.next = s.next;
            s.next = M;
            break;
        }
        else {
            s = s.next;
        }
    }
    System.out.println();
    //遍历单链表,打印每个结点的
    s = S1;
    while (s != null) {
        System.out.println(s.data);
        s = s.next;
    }
    System.out.println();
    //从链表中删除自己
    s = S1;
    while (s != null) {
        if (s.next.data == 20165303) {
            s.next = s.next.next;
            break;
        }
        else {
            s = s.next;
        }
    }
    System.out.println();
    //遍历单链表,打印每个结点的
    s = S1;
    while (s != null) {
        System.out.println(s.data);
        s = s.next;
    }
}

}

homework

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325518728&siteId=291194637