2015-10-16 1 views
0

Comme le suggère le titre, je suis en train de trier mes ArrayList d'objets, et ces objets ont mis en œuvre comparables et ont overriden CompareTone peut pas trier une liste de tableau à l'aide Collections.sort après la mise en œuvre Comparable

Cependant, quand je suis arrivé à trier mon arraylist je reçois cette erreur enter image description here Je n'arrive pas à comprendre pourquoi je reçois ce problème? Quelqu'un peut-il aider?

ItemList Classe:

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 Classe:

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

Montrez-nous les codes s'il vous plaît? –

+0

vous ne pouvez pas avoir utilisé le type d'élément dans la méthode 'compareTo' ou' implements Comparable' – SacJn

+0

J'ai ajouté mon code ci-dessus :) @SacJn – RandomMath

Répondre

0

En bref, l'article ne met pas en œuvre Comparable. Et la liste est une liste d'article.

Détails ici. Dans le code ci-dessus, items = new ArrayList<Item>();. Ce qui signifie que le compilateur sait seulement que dans les éléments de la liste, le type est Item. Donc, quand invoquer Collections.sort(items), le compilateur java trouvé élément ne met pas en œuvre comparable. Et il y aura une erreur comme indiqué au début.

Comment réparer? Modifier la définition de l'élément, le rendre implémentable comparable.

Comme vous pouvez le voir, il n'y a rien à propos de SingleItem. Parce que c'est l'impl et les éléments ne voient que l'élément d'interface.