2016-05-15 74 views
0

我想用泛型對鏈表進行排序,但是我遇到了一些鑄造問題。代碼拋出總線不能被投射到節點。我知道問題出在比較器中(我轉到Bus),否則我不知道如何調用Bus中定義的方法(無論是Bus還是其他對象,只是隨機測試)。我一直在研究互聯網,但找不到解決方案。下面是代碼:使用比較器和泛型對鏈表進行排序java

/** 
* Swaps the current node's element with the previous one 
*/ 
public void swap(){ 
    Object previous = getCurrent().getElement(); 
    Object current = next().getElement(); 
    getCurrent().setElement(previous); 
    previous().setElement(current); 
} 

public AbstractList<T> orderBy(Comparator<Node<T>> comparator){ 
    setCurrent(getFirst()); 
    Node<T> aux; 
    Node<T> current; 
    boolean check = true; 
    while (check){ 
     check = false; 
     aux = getFirst(); 
     current = getFirst().getNext(); 
     while(hasNext()) { 
      if (comparator.compare(aux, current) > 0) { 
       check = true; 
       swap(); 
      } 
      aux = current; 
      current = current.getNext(); 
     } 
    } 
    return this; 
} 

比較:

import java.util.Comparator; 
public class InternComparator implements Comparator<Node<Bus>>{ 

@Override 
public int compare(Node<Bus> o1, Node<Bus> o2) { 
return ((Bus)o1.getElement()).getIntern() - ((Bus)o2.getElement()).getIntern(); 
     } //getIntern() returns an Integer 

AbstractList中(由教授提供):

import java.util.NoSuchElementException; 
public class AbstractList<T> { 
    private Node<T> first; 
    private Node<T> current; 
    private Node<T> last; 
    private int size = 0; 


    public boolean hasNext(){ 
     return current != last; 
    } 

    public boolean hasPrevious(){ 
     return current != first; 
    } 

    public Node getCurrent(){ 
     return current; 
    } 

    public void setCurrent(Node<T> current) { 
     this.current = current; 
    } 

    public int size(){ 
     return size; 
    } 

    public boolean isEmpty(){ 
     return first == last || first == null; 
    } 

    public void add(T t){ 
     Node<T> a = new Node<T>(t); 
     if(first == null){ 
      first = a; 
     } 
     if(last == null && first != null){ 
      last = a; 
      last.setPrevious(first); 
      first.setNext(last); 
     } 

     Node<T> aux = last; 
     last.setNext(a); 
     last = a; 
     last.setPrevious(aux); 
     current = last; 
     size++; 
    } 

    public Node next(){ 
     if(!hasNext()){ 
      throw new RuntimeException("No elements available next."); 
     } 
     current = current.getNext(); 
     return current; 
    } 

    public Node previous(){ 
     if(!hasPrevious()){ 
      throw new RuntimeException("No elements available previous."); 
     } 
     current = current.getPrevious(); 
     return current; 
    } 

    public Node getFirst(){ 
     return first; 
    } 

    public Node getLast(){ 
     return last; 
    } 

    public void setFirst(Node<T> first){ 
     this.first = first; 
    } 

    public void setLast(Node<T> last) { 
     this.last = last; 
    } 

    public void remove(T t){ 
     current = first; 
     boolean removed = false; 
     while (hasNext()) { 

      if (current.getElement() == t || current.getElement().equals(t)) { 
       if(current != last && current != first) { 
        current.getNext().setPrevious(current.getPrevious()); 
        current.getPrevious().setNext(current.getNext()); 
       } else if(current == first){ 
        current.getNext().setPrevious(null); 
       } else if(current == last){ 
        current.getPrevious().setNext(null); 
       } 
       removed = true; 
       return; 
      } 

      current = next(); 
     } 

     if(removed){ 
      size--; 
     } else { 
      throw new NoSuchElementException("No such element on the list."); 
     } 
    } 

    public Node goTo(int pos){ 
     if(pos > size){ 
      throw new ArrayIndexOutOfBoundsException("Inexistent Position"); 
     } 
     current = first; 
     for(int i = 0; i < pos; i++){ 
      current = next(); 
     } 
     return current; 
    } 

    public void insertAfter(T t){ 
     Node<T> aux = new Node<>(t); 
     if(current != last) { 
      Node<T> next = current.getNext(); 
      Node<T> previous = current; 
      current.getNext().setPrevious(aux); 
      current.setNext(aux); 
      aux.setNext(next); 
      aux.setPrevious(previous); 
     } else { 
      current.setNext(aux); 
      aux.setPrevious(current); 
      last = aux; 
     } 
     size++; 
    } 

    public void insertPrevious(T t){ 
     Node<T> aux = new Node<>(t); 
     if(current != first) { 
      Node<T> previous = current.getPrevious(); 
      Node<T> next = current; 
      current.getPrevious().setNext(aux); 
      current.setPrevious(aux); 
      aux.setNext(next); 
      aux.setPrevious(previous); 
     } else { 
      Node<T> aux1 = current; 
      current.setPrevious(aux); 
      first = aux; 
      first.setNext(aux1); 
     } 
     size++; 
    } 

    @Override 
    public String toString() { 
     String result = ""; 
     current = first; 
     while(hasNext()) { 
      result += current.getElement() + ""; 
      next(); 
     } 
     return result; 
    } 
} 

class Node<T> { 
    private Object element; 
    private Node<T> prev; 
    private Node<T> next; 

    public Node(T element){ 
     this.element = element; 
    } 

    public Node(T element, Node next){ 
     this.element = element; 
     this.next = next; 
    } 

    public Node<T> getNext() { 
     return next; 
    } 

    public Node<T> getPrevious() { 
     return prev; 
    } 

    public Object getElement() { 
     return element; 
    } 

    public void setNext(Node<T> next) { 
     this.next = next; 
    } 

    public void setPrevious(Node<T> prev) { 
     this.prev = prev; 
    } 

    public void setElement(Object element) { 
     this.element = element; 
    } 
} 
+0

我快速捲土重來:-) – GhostCat

回答

1

再想想:你的目標是Node<Bus>類型;你想知道爲什麼演員Bus失敗?

或讓我們改述:你認爲Bus<People>代表人類嗎?

如果你有一個容器,那麼你必須檢索包含的值;這不能通過鑄造容器來實現!

或者,爲了繼續使用奇怪的圖片:通過將盒子聲明爲雞蛋,您不會獲得一個雞蛋。你打開盒子,取出蛋。

+0

這不是我第一次嘗試,我已經試過比較器,但遲早會遇到同樣的問題。 –

+0

我看到了..問題是:你貼了很多代碼;但仍然不夠,所以其他人可以重現正在發生的事情。例如:沒有代碼顯示你如何填充你的列表。長話短說:嘗試拿出一個編譯和運行的最小示例...並注意:考慮研究TDD和單元測試。代碼,因爲這是**完美**寫這樣的方式;然後你通常不會遇到這樣的問題。 – GhostCat