-3

I am working on an assignment for a programming course I am following and I am using a List to store data. The List class:

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;
}

Now the assignment is to make the list a sorted list, sorting the items from large to small. I need to do this in a separate class called SortedList.

I made a start, but I have really no idea on what to do next:

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

}

I am using the list in my program for two different objects: I use the list in my Set class. A set is basically a collection of natural numbers. For example: {1,2,3,4,5} is a set.

Furthermore, I use the list in my Table class. The table consists of Variables. A variable consists of a key and a value. The key is an identifier (Alfa for example) and the value is a Set {1,2,3}. The assignment is to order the items in the list from big to small.

So the SortedList needs to be a separate class that extends the list class! How can I do this? Many many thanks!

  • which item do you want to be sorted ? – Sufiyan Ghori Jan 29 '15 at 21:46
  • 2
    You can do an insertion sort, looking down the list until you find an element which is less than the one you are adding. This is your insertion point. – Peter Lawrey Jan 29 '15 at 21:47
  • I am using the list in my program for two different objects: I use the list in my Set class. A set is basically a collection of natural numbers. For example: {1,2,3,4,5} is a set. Furthermore, I use the list in my Table class. The table consists of Variables. A variable consists of a key and a value. The key is an identifier (Alfa for example) and the value is a Set {1,2,3}. The assignment is to order the items in the list from big to small. How can I do this? Many many thanks! – Dennis Kraakman Jan 29 '15 at 21:58

2 Answers2

0

You do not want to compare two Lists (which is what List implements Comparable implies) but to compare the lists elements -- that means, the elements have to be Comparable not the List(-type).

What makes a List a SortedList is that upon insertion of a new element, that particular element gets inserted at the correct position in the list.

Tedil
  • 1,935
  • 28
  • 32
  • I am using the list in my program for two different objects: I use the list in my Set class. A set is basically a collection of natural numbers. For example: {1,2,3,4,5} is a set. Furthermore, I use the list in my Table class. The table consists of Variables. A variable consists of a key and a value. The key is an identifier (Alfa for example) and the value is a Set {1,2,3}. The assignment is to order the items in the list from big to small. How can I do this? Many many thanks! – Dennis Kraakman Jan 29 '15 at 21:59
  • "The assignment is to order the items in the list from big to small" - Just as I said and @Peter Lawrey commented: the 'easiest' way is to keep your list sorted by inserting new elements at the correct insertion point¹. That way your list is always sorted (i.e. you only need to change the `insert` method.) ¹ Don't forget you already got a `find` method which might prove useful here. – Tedil Jan 29 '15 at 22:03
0

To sort a list in Java you can use two interfaces: comparable and comparator.
You can use comparator, comparable or both.

Comparable is the default sort option.
Comparator can be replaced with another one, that sorts by a different value.


    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;
    }
}
Andie2302
  • 4,825
  • 4
  • 24
  • 43