Question

I have an array list of integers and would like to delete an Integer value from it. To clarify: Let's say I have a function that takes in an ArrayList of Integers, and specified Integer value. I need to return the list with (value+1) deleted from it.

boolean deleteFromList(ArrayList<Integer> list, Integer value)

Now, if I do this:

return list.remove(value+1)

compiler will complain because it will try to invoke the delete method that takes int parameter and deletes an object from specified location, not the actual object. So what is a proper way to deal with this? Is it better to do:

list.remove((Integer)(value+1))

or

int v = value.intValue();
v++;
list.remove(new Integer(v)); 

? In the second case, can I be sure the right value will be deleted?

Was it helpful?

Solution 2

In the second case, can I be sure the right value will be deleted?

Yes you can.

The remove method will use equals(Object) to identify the object to be removed from the list. Since Integer.equals compares by value (not by object identity), it doesn't matter how you created the Integer instances ... provided the wrapped int values are the same.


However ... your second version is inferior to the first version:

  • It cumbersome: 3 statements instead of 1.

  • You are using new Integer(...) rather than Integer.valueOf(...) which is always going to create a new instance. By contrast, autoboxing (or calling Integer.valueOf(...) explicitly) makes use of the Integer classes instance cache, and that will often avoid creating a new instance.

OTHER TIPS

list.remove(value+1) will remove at given index.

You can use list.remove(Integer.valueOf(value+1)) to remove the Integer.

Integer.valueOf(v) is recommanded instead of new Integer(v) because it allows reuse of Integer instances for special values (see javadoc).

EDIT : In term of boxing/unboxing, it is possible to completely eliminate the problem using libraries like trove4j which define an dynamic array of integer storing the primitive values instead of numeric class (see TIntArrayList). No boxing, no unboxing, lower GC use and better performance.

list.remove(Integer.valueOf(value+1));

should work fine.

Do you looking for this one.

Collections.sort(list);
int index = list.indexOf(5);
list = list.subList(0, index-1);
System.out.println(list);

You can use list.indexOf(value + 1) since that method only takes the object. Then use that index to remove the element.

    int i = list.indexOf(value + 1);
    list.remove(i);

If you need to remove all instances of value + 1 then continue until i is -1 (not found).

Or, just iterate over it and remove as you find them

    for (Iterator<Integer> iterator = list.iterator(); iterator.hasNext(); ) {
        Integer integer = iterator.next();
        if (integer == value + 1) {
            iterator.remove();
        }
    }

A more effective way might be to use a TIntArrayList from Trove4j library which wraps int[] array and doesn't use wrapper types. For this reason it takes 3x times less memory and is much faster.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top