如何使列表成为排序列表?

时间:2015-01-29 21:43:58

标签: java list sorting comparable sorted

我正在为我正在关注的编程课程做作业,我正在使用List来存储数据。 List类:

public List()   {
    init();
}

protected Node<E> first, current, last;
public int numberOfNodes;

public boolean isEmpty() {
    return numberOfNodes == 0;
}

public List<E> init() {
    numberOfNodes = 0;
    first = current = last = null;
    return this;
}

public int size() {
    return numberOfNodes;
}

public List<E> insert(E d) {
    E copy = (E)d.clone(); 
    if (isEmpty()) { 
        first = current = last = new Node(copy); 
        numberOfNodes += 1;
        return this;
    }
    else{
        for (current = first; current != null; current = current.next){
            if(current.next== null){ 
                current.next = last = new Node(copy);
                last.prior = current;
                last.next = null;
                numberOfNodes += 1;
                return this;
            }
            else{
                Node<E> newNode = new Node(copy);
                current.next.prior = newNode;
                newNode.next = current.next;
                newNode.prior = current;
                current.next = newNode;
                current = newNode;
                numberOfNodes +=1;
                return this;
            }
        }
    }
    return this;
}

public E retrieve() {
    return (E) current.data.clone();
}

public List<E> remove() {       
    if (isEmpty()){
        return init();
    }
    else if (numberOfNodes == 1){
        return init();
    }
    else if (current == first) {
        first = current = current.next;
        current.prior = null;
        numberOfNodes -= 1;
    } 
    else if (current == last) {
        last = current = current.prior;
        current.next = null;
        numberOfNodes -= 1;
    } 
    else {
        current.prior.next = current.next;
        current.next.prior = current.prior;
        current = current.next;
        numberOfNodes -= 1;
    }
    return this;
}

public boolean find(E d) {
    current = first;
    while((current!=null && !(d.compareTo(current.data)==0))){
        current=current.next;
    }
    if (current==null){
        return false;
    }else{
        return true;
    }
}

public boolean setFirst() {
    if(isEmpty()){
        return false;
    }
    else{
        current = first;
        return true;
    }
}

public boolean setLast() {
    if(isEmpty()){
        return false;
    }
    else{
        current = last;
        return false;
    }
}

public boolean getNext() {
    if(isEmpty()||current == last){
        return false;
    }
    else{
        current = current.next;
        return true;
    }
}

public boolean getPrior() {  
    if(isEmpty()||current == first){
        return false;
    }
    else{
        current = current.prior;
        return true;
    }
}

public List<E> clone() {
    List<E> clone;
    try{
        clone = (List<E>)super.clone();
    } catch(CloneNotSupportedException e){
        throw new Error("This cannot be cloned!");
    }
    clone.init();
    for(Node n = first; n != null; n = n.next){
        clone.insert((E)n.clone().data);
    }
    clone.numberOfNodes = this.numberOfNodes;
    return clone;
}

现在,分配是使列表成为一个排序列表,将项目从大到小排序。我需要在一个名为SortedList的单独类中执行此操作。

我开始了,但我真的不知道下一步该做什么:

public class SortedList extends List implements Comparable {
public int compareTo(Object o) {
    // TODO Auto-generated method stub
    return 0;
}

}

我在程序中使用两个不同对象的列表: 我在我的Set类中使用列表。集合基本上是自然数的集合。例如:{1,2,3,4,5}是一个集合。

此外,我使用Table类中的列表。该表由变量组成。变量由键和值组成。键是标识符(例如Alfa),值是Set {1,2,3}。分配是将列表中的项目从大到小排序。

所以SortedList需要是一个扩展列表类的独立类! 我怎样才能做到这一点?非常感谢!

2 个答案:

答案 0 :(得分:0)

您不想比较两个列表(这是List implements Comparable所暗示的),而是比较列表元素 - 这意味着元素必须是Comparable不是List( - 类型)。

列表是SortedList的原因是在插入新元素时,该特定元素会在列表中的插入

答案 1 :(得分:0)

要用Java对列表进行排序,您可以使用两个接口:可比较和比较 您可以使用比较器,可比较或两者兼得。

可比较是默认排序选项 比较器可以用另一个替换,用不同的值排序。


    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;

    public class Demo {

    private ArrayList<MyObject> list = new ArrayList<>();

    public Demo() {
        init();
        //Collections.sort(list); //Default sort.
        //Collections.sort(list, new SortByText());
        //Collections.sort(list, new SortByNumberDesc());
        output();
    }

    public void init() {
        list.add(new MyObject(100, "Hello4", 654.423));
        list.add(new MyObject(344, "Hello1", 65.423));
        list.add(new MyObject(3465, "Hello3", 65.23));
        list.add(new MyObject(43, "Hello8", 6523));
        list.add(new MyObject(87, "Hello2", 654.423));
        list.add(new MyObject(12432, "Hello5", 0.423));
        list.add(new MyObject(-432, "Hello7", 65.3));
        list.add(new MyObject(-5, "Hello6", 8979.487));
        list.add(new MyObject(10, "Hello9", 549.2));
    }

    public void output() {
        for (Iterator<MyObject> iterator = list.iterator(); iterator.hasNext();) {
            MyObject next = iterator.next();
            System.out.println(next.getOutput());
        }
    }

    public static void main(String[] args) {
        new Demo();
    }

}

public class MyObject implements Comparable<MyObject> {

    private int index;
    private String text;
    private double number;

    public MyObject(int index, String text, double number) {
        this.index = index;
        this.text = text;
        this.number = number;
    }

    public String getText() {
        return text;
    }

    public int getIndex() {
        return index;
    }

    public double getNumber() {
        return number;
    }

    @Override
    public int compareTo(MyObject o) {
        if (index > o.index) {
            return 1;
        }
        if (index < o.index) {
            return -1;
        }
        return 0;
    }

    public String getOutput() {
        return "index: " + index + " text: " + text + " number2: " + number;
    }

}

import java.util.Comparator;

public class SortByText implements Comparator<MyObject> {

    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getText().compareTo(o2.getText());
    }
}

    import java.util.Comparator;

    public class SortByNumberDesc implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        if (o1.getNumber() > o2.getNumber()) {
            return -1;
        }
        if (o1.getNumber() < o2.getNumber()) {
            return 1;
        }
        return 0;
    }
}