Java compareTo方法错误:java.lang.Integer无法强制转换为java.lang.Boolean

时间:2017-01-15 05:31:03

标签: java compareto

当我尝试对两个可比项目进行比较时,下面的代码会抛出此错误:

  

java.lang.Integer无法强制转换为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中只有一个名为“list”的Integer对象。

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

有人可以解释那里发生了什么吗?

4 个答案:

答案 0 :(得分:4)

java.util.List 有两个重载的remove方法:一个采用int(索引)并返回已删除的元素,另一个采用{{1如果列表包含指定的元素,则返回Objectboolean,否则返回true)。

您的代码会调用false方法,并且您尝试将其remove(Object)返回值添加到您的列表中:boolean

以下是两个重载方法的Javadoc:

list.add(j, list.remove(current));

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

答案 1 :(得分:2)

更改

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

答案 2 :(得分:1)

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

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

}

答案 3 :(得分:0)

在您的情况下,您应该使用Remove metnod和索引器。 Už的意思是,您可以将 index i 用作可比较Remove(它是secont变体Remove(your index i))函数的参数,它应该可以工作。希望它有所帮助。