2017-01-15 1 views
1

Lorsque je tente de faire une comparaison entre deux éléments comparables, le code ci-dessous jette cette erreur:méthode Java compareTo Erreur: java.lang.Integer ne peut pas être jeté à java.lang.Boolean

java.lang.Integer cannot be cast to java.lang.Boolean

ArrayList<Comparable> list = new ArrayList<Comparable>(); 

public void myMethod(ArrayList <Comparable> list) { 
    for(int i = 1; i < list.size()-1;i++){ 
     //Comparable current = list.get(i); 
     int j = i; 
     while((j > -1) && ((list.get(j-1).compareTo(list.get(i))) < 0)){ 
      j--; 
     } 
     list.add(j, list.remove(list.get(i))); 
    } 
    System.out.println(list); 
} 

Le but de la ligne suivante est de comparer deux l'objet courant dans la liste au précédent. Notez qu'il existe uniquement des objets Integer dans la liste de tableaux appelée 'list'.

while(((list.get(j - 1).compareTo(current)) < 0) && (j > -1)) 

Quelqu'un peut-il expliquer ce qui se passe là-bas?

+1

Quel est le type de données pour la liste? – Suraj

+1

Il manque trop d'informations dans votre question pour permettre une réponse précise. S'il vous plaît fournir un MCVE approprié –

+0

Objets entiers – Matt

Répondre

4

java.util.Lista deux surchargé remove méthodes: on prend un int (index) et renvoie l'élément retiré, l'autre prend un Object et renvoie un boolean (true si la liste contient l'élément spécifié, sinon false).

code Vous appelle la méthode remove(Object), et que vous essayez d'ajouter sa valeur boolean de retour à votre liste: list.add(j, list.remove(current));

Voici le Javadoc pour les deux méthodes surchargées:

/** 
* Removes the element at the specified position in this list (optional 
* operation). Shifts any subsequent elements to the left (subtracts one 
* from their indices). Returns the element that was removed from the 
* list. 
* 
* @param index the index of the element to be removed 
* @return the element previously at the specified position 
* @throws UnsupportedOperationException if the <tt>remove</tt> operation 
*   is not supported by this list 
* @throws IndexOutOfBoundsException if the index is out of range 
*   (<tt>index &lt; 0 || index &gt;= size()</tt>) 
*/ 
E remove(int index); 

et:

/** 
* Removes the first occurrence of the specified element from this list, 
* if it is present (optional operation). If this list does not contain 
* the element, it is unchanged. More formally, removes the element with 
* the lowest index <tt>i</tt> such that 
* <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt> 
* (if such an element exists). Returns <tt>true</tt> if this list 
* contained the specified element (or equivalently, if this list changed 
* as a result of the call). 
* 
* @param o element to be removed from this list, if present 
* @return <tt>true</tt> if this list contained the specified element 
* @throws ClassCastException if the type of the specified element 
*   is incompatible with this list (optional) 
* @throws NullPointerException if the specified element is null and this 
*   list does not permit null elements (optional) 
* @throws UnsupportedOperationException if the <tt>remove</tt> operation 
*   is not supported by this list 
*/ 
boolean remove(Object o); 
+1

Salut, merci pour votre réponse, j'ai une question cependant: ne pas list.remove (item) le retirer de la liste et le renvoyer aussi? Dans quel scénario reviendrait-il un booléen? – Matt

+1

Merci, je comprends maintenant, je vais consulter les javadocs plus attentivement la prochaine fois – Matt

2

Modifier

list.add(j, list.remove(list.get(i))); // this remove returns a boolean 
> Removes the first occurrence of the specified element from this list, 
>  * if it is present. If the list does not contain the element, it is 
>  * unchanged. More formally, removes the element with the lowest index 
>  * <tt>i</tt> such that 
>  * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt> 
>  * (if such an element exists). Returns <tt>true</tt> if this list 
>  * contained the specified element (or equivalently, if this list 
>  * changed as a result of the call). 
>  * 
>  * @param o element to be removed from this list, if present 
>  * @return <tt>true</tt> if this list contained the specified element 

à

list.add(j, list.remove(i)); //this remove returns a Comparable 
> Removes the element at the specified position in this list. 
>  * Shifts any subsequent elements to the left (subtracts one from their 
>  * indices). 
>  * 
>  * @param index the index of the element to be removed 
>  * @return the element that was removed from the list 
+1

Merci, je comprends maintenant. C'était utile – Matt

1

En regardant votre question, il semble que vous voulez ne conserver que des valeurs entières uniques dans votre liste. Si c'est le cas, quelque chose comme ça fonctionnerait.

public void getUniqueList(ArrayList<> list){ 
    for(int i = 1; i < list.size()-1;i++){ 
      for(int j = i + 1; j < list.size(); j++){ 
       if (list.get(j) == list.get(j - 1)) { 
        list.remove(j)  //this removes the value if it already appeared the list 
       } 
      } 
     } 
    System.out.print(list); 

} 
0

Dans votre cas, vous devez utiliser Remove metnod avec indexeur. Už signifie, vous pouvez utiliser votre indice i comme paramètre comparable Remove (c'est la variante secont variante Remove(your index i)) et il devrait être travail. J'espère que ça aide.