Properly removing an Integer from a List<Integer>
Java always calls the method that best suits your argument. Auto boxing and implicit upcasting is only performed if there's no method which can be called without casting / auto boxing.
The List interface specifies two remove methods (please note the naming of the arguments):
remove(Object o)
remove(int index)
That means that list.remove(1)
removes the object at position 1 and remove(new Integer(1))
removes the first occurrence of the specified element from this list.
How to remove integer from list?
This is normal, there are two versions of the .remove()
method for lists: one which takes an integer as an argument and removes the entry at this index, the other which takes a generic type as an argument (which, at runtime, is an Object
) and removes it from the list.
And the lookup mechanism for methods always picks the more specific method first...
You need to:
list.remove(Integer.valueOf(300));
in order to call the correct version of .remove()
.
Java, how to remove an Integer item in an ArrayList
try this
list.removeAll(Arrays.asList(2));
it will remove all elements with value = 2
you can also use this
list.remove(Integer.valueOf(2));
but it will remove only first occurence of 2
list.remove(2)
does not work because it matches List.remove(int i)
which removes element with the specified index
Remove From ArrayList - Remove an integer from the list
For scenario 1, use remove(int index)
. Since the third item has index 2
(it is zero-based), you may remove it like this:
// Scenario 1:
numbers.remove(2);
For scenario 2, use remove(Object o)
. Just cast it to Integer
and it will search for the object that equals 3:
// Scenario 2:
numbers.remove((Integer)3);
Test it:
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
// Scenario 1:
numbers.remove(2);
// Scenario 2:
numbers.remove((Integer)3);
System.out.println(numbers);
Note: In both scenarios, there is no need to iterate the list.
Update: In scenario 2, if the list may contain more than one number 3, then to remove all of them (Java 8) I would do:
numbers.removeIf(((Integer)3)::equals);
Removing integers from a list+python
The problem is:
- You are iterating over the list as you remove items from it, and
- Your counter variable
x
is not taking into account the positions of removed items.
Try this:
list_a = ["Michell",123,"Apple","Food",456,3231,"Another"]
list_e = []
x = 0
for item in list_a[:]:
print "x is: ", x
print "item is: ", item, type(item)
if isinstance(item,int):
list_e.append(item)
list_a.pop(list_a.index(item))
x+=1
print list_a
print list_e
See also:
(1), (2), (3)
Removing an integer from a list
List<Integer> list = new ArrayList<Integer>(Arrays.asList(5, 10, 42));
if (list.contains(10)) {
list.remove(10); // IOOBE
}
The problem with the above code is that you're actually not calling List#remove(Object)
but List#remove(int)
, which removes the element at given index (and there's no element at index 10).
Use instead:
List<Integer> list = new ArrayList<Integer>(Arrays.asList(5, 10, 42));
if (list.contains(10)) {
list.remove((Integer) 10);
}
That way, you force the compiler to use the List#remove(Object)
method.
Related Topics
In Java, Is the Result of the Addition of Two Chars an Int or a Char
Does Java Support Default Parameter Values
How to Create a Windows Service from Java App
How to Convert Json to a Hashmap Using Gson
What Does ≪T≫ (Angle Brackets) Mean in Java
Including Jars in Classpath on Commandline (Javac or Apt)
Difference Between ≪Context:Annotation-Config≫ and ≪Context:Component-Scan≫
How to Import an Existing X.509 Certificate and Private Key in Java Keystore to Use in Ssl
What Are Static Factory Methods
Hashmap With Multiple Values Under the Same Key
Take a Char Input from the Scanner
How to Use a Servlet Filter in Java to Change an Incoming Servlet Request Url
To Prevent a Memory Leak, the Jdbc Driver Has Been Forcibly Unregistered