Search code examples
javacompareto

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


When I try to make a comparison between two comparable items, the code below throws this error:

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

The purpose of the following line is to compare two the current object in the list to the previous one. Note that there are ONLY Integer objects in the ArrayList called 'list'.

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

Can anyone explain what's going on there?


Solution

  • java.util.List has two overloaded remove methods: one takes a int (index) and returns the removed element, the other one takes an Object and returns a boolean (true if the list contained the specified element, otherwise false).

    You code invokes the remove(Object) method, and you're trying to add its boolean return value to your list: list.add(j, list.remove(current));

    Here is the Javadoc for the two overloaded methods:

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

    and:

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