2015-10-16 106 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。