Java compareTo method Error :java.lang.Integer cannot be cast to java.lang.Boolean

Java compareTo method Error :java.lang.Integer cannot be cast to 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);
}

下一行的目的是将列表中的两个当前对象与前一个对象进行比较。 请注意,ArrayList 中只有 Integer 对象称为 'list'。

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

谁能解释一下那里发生了什么?

java.util.List 有两个重载的remove方法:一个是int(索引)和returns元素,另一个采用 Object 和 return 一个 boolean(如果列表包含指定元素,则为 true,否则为 false)。

您的代码调用了 remove(Object) 方法,并且您正在尝试将其 boolean return 值添加到您的列表中:list.add(j, list.remove(current));

这是两个重载方法的 Javadoc:

/**
 * 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);

和:

/**
 * 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);

改变

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

通过查看您的问题,您似乎只想在列表中保留唯一的整数值。如果是这样的话,这样的事情就可以了。

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);

}

在您的情况下,您应该将 Remove metnod 与索引器一起使用。 Už 确实意味着,您可以使用 index i 作为参数 comparable Remove(它是第二个变体 Remove(your index i))函数,它应该可以工作。希望对你有帮助。