2015-10-16 65 views
0

正如標題所暗示的,我想我的排序對象的ArrayList中,這些對象已實現媲美,並重寫的CompareTo無法排序使用Collections.sort一個數組列表,可比

但是當我到排序我arraylist我得到這個錯誤enter image description here 我似乎無法理解爲什麼我得到這個問題?任何人都可以幫忙嗎?

ITEMLIST類:

package stories.cs2800; 

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

import javax.swing.AbstractListModel; 

@SuppressWarnings("serial") 
public class ItemList extends AbstractListModel<Item> { 

    private ArrayList<Item> items; 

    /** 
    * Constructor that initializes the array list. 
    */ 
    public ItemList() { 
    items = new ArrayList<Item>(); 
    } 

    @Override 
    public int getSize() { 
    return items.size(); 
    } 

    /** 
    * Method to get item based on the index. 
    * @param argIndex - Takes parameter of type int. 
    * @return Returns item at the index. 
    */ 
    @Override 
    public Item getElementAt(int argIndex) throws IndexOutOfBoundsException { 
    if (argIndex < 0 || argIndex >= items.size()) { 
     throw new IndexOutOfBoundsException(); 
    } 
    return items.get(argIndex); 
    } 

    /** 
    * Method that gets and Item element based on the name. 
    * 
    * @param argName - takes parameter of type String. 
    * @return Returns the entire item object if name matches, else null. 
    */ 
    public Item getElementByName(String argName) { 
    for (Item i : items) { 
     if (i.getName().equals(argName)) { 
     return i; 
     } 
    } 
    return null; 
    } 

    /** 
    * Method to add another Item into the array list. 
    * @param Takes parameter of type item. 
    * @see ArrayList#add 
    */ 
    public void add(Item next) { 
    items.add(next); 
    Collections.sort(items); 
    } 

    /** 
    * Boolean method to check if list contains the item of type Item. 
    * 
    * @param Takes parameter of type item. 
    * @return returns boolean value of true or false if item is contained. 
    */ 
    public Boolean contains(Item argItem) { 
    return items.contains(argItem); 
    } 

    /** 
    * Boolean method remove to remove item of type Item from list. 
    * 
    * @param Takes parameter of type Item. 
    * @return returns boolean value of true or false if item is removed. 
    */ 
    public Boolean remove(Item argItem) { 
    return items.remove(argItem); 
    } 

    /** 
    * Boolean method that removes and item based on its index. 
    * 
    * @argIndex Takes a parameter of type int. 
    * @return returns boolean value of true or false if item was removed based on index. 
    */ 
    public Boolean remove(int argIndex) throws IndexOutOfBoundsException { 
    if (argIndex < 0 || argIndex >= items.size()) { 
     throw new IndexOutOfBoundsException(); 
    } 
    return items.remove(items.get(argIndex)); 
    } 

    /** 
    * Method to find the index of an item object. 
    * 
    * @param Takes parameter of type Item. 
    * @return Returns item index based on the item object entered. 
    */ 
    public int indexOf(Item argItem) { 
    return items.indexOf(argItem); 
    } 

    /** 
    * Method to check if item changed. 
    * 
    * @param Takes parameter of type Item. 
    */ 
    public void changed(Item argItem) { 
    fireContentsChanged(this, items.indexOf(items), items.indexOf(items)); 
    /*Method called when one or more items have changed */ 
    } 
} 

SingleItem類:

package stories.cs2800 
    public class SingleItem implements Comparable<Item>, Item { 

    private String name; 
    private float value; 
    private Item child; 

    /** 
    * Constructor for the SingleItem object. 
    * 
    * @param argName Stores the name that is set in constructor 
    * @param argValue Stores the float value that is set in Constructor 
    * @param argChild Stores the value of the child element 
    */ 
    public SingleItem(String argName, float argValue, Item argChild) { 
    name = argName; 
    value = argValue; 
    child = argChild; 
    } 

    /** 
    * Getter method to get the value of the name variable. 
    * 
    * @return returns the name 
    */ 
    @Override 
    public String getName() { 
    return this.name; 
    } 

    /** 
    * Getter method to get the value of the Float variable. 
    * 
    * @return value set in the value variable 
    * 
    */ 
    @Override 
    public Float getValue() { 
    return this.value; 
    } 

    /** 
    * Setter method to set the value of the Float - No return type. 
    * @param argFloat - Takes parameter of type Float using Wrapper class. 
    */ 
    @Override 
    public void setValue(Float argFloat) { 
    this.value = argFloat; 
    } 

    /** 
    * Method to get the description. 
    * 
    * @return Returns the a string with the description 
    */ 
    @Override 
    public String getDescription() { 
    return "Single items have no description"; 
    } 

    /** 
    * Getter method for child element. 
    * 
    * @return returns the value of child element 
    */ 
    public Item getChild() { 
    return this.child; 
    } 

    /** 
    * Method for adding items. 
    * @param child - Takes parameter of type Item. 
    * @return Returns Exception when child is null. 
    * 
    */ 

    @Override 
    public void add(Item child) { 
    if (this.child != null) { 
     throw new IllegalStateException(); 
    } 
    this.child = child; 
    } 

    /** 
    * Method for ItemVisitor. 
    * @param argVisitor - Takes parameter of ItemVisitor to add current class. 
    * @return Currently no valid method!. 
    * 
    */ 
    @Override 
    public <T> void accept(ItemVisitor<T> argVisitor) { 
    argVisitor.add(this); 
    } 

    /** 
    * Method that takes Boolean as a parameter. 
    * @param argBool - Takes parameter of type boolean. 
    * @return Returns exception. 
    */ 
    @Override 
    public void open(boolean argBool) throws IllegalStateException { 
    throw new IllegalStateException(); 
    } 

    /** 
    * Method that compares name to name entered as a parameter. 
    * @param item - Takes parameter of type item. 
    * @return Returns an int based on comparison of name. E.g. 0 = same, 1 = different. 
    */ 
    @Override 
    public int compareTo(Item item) { 
    return this.name.compareTo(item.getName()); 
    } 

    /** 
    * Method to check if Open. 
    * 
    * @return Always returns true. 
    */ 
    @Override 
    public boolean isOpen() { 
    return true; 
    } 

    @Override 
    public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((name == null) ? 0 : name.hashCode()); 
    return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
    if (this == obj) { 
     return true; 
    } 
    if (obj == null) { 
     return false; 
    } 
    if (getClass() != obj.getClass()) { 
     return false; 
    } 
    SingleItem other = (SingleItem) obj; 
    if (name == null) { 
     if (other.name != null) { 
     return false; 
     } 
    } else if (!name.equals(other.name)) { 
     return false; 
    } 
    return true; 
    } 

    /** 
    * toString method to get values of elements. 
    * 
    * @return Returns a sentence with the values of all elements. 
    */ 
    @Override 
    public String toString() { 
    return "Name: " + getName() + ", Value: " + getValue() 
     + ", Description: " 
     + getDescription(); 
    } 
} 
+0

請向我們顯示密碼? –

+0

您可能沒有在'compareTo'方法中使用過類型的項目或'implements Comparable' – SacJn

+0

我已經在上面添加了我的代碼:) @SacJn – RandomMath

回答

0

總之,項目沒有實現可比。列表是一個Item的列表。

詳情在這裏。在上面的代碼中,items = new ArrayList<Item>();。這意味着編譯器只知道在列表項中,類型是Item。所以當調用Collections.sort(items)時,java編譯器發現Item沒有實現Comparable。開始時會顯示一個錯誤。

如何解決?更改Item的定義,使其實現Comparable。

所以,你可以看到,沒有關於SingleItem。因爲它是impl和items只能看到接口Item。