+ 5

Is there any library method to remove an integer element from a ArrayList of integers in case we don't know its first occurance?

Overloads of ArrayList.remove() in Library :- remove(int index) : Removes the element at the specified position in this list. remove(Object o): Removes the first occurrence of the specified element from this list, if it is present.

29th Mar 2018, 4:56 PM
Sayan Kundu
6 Antworten
+ 3
ArrayList.remove ((Integer)data) is the a way to do this task. Widening into non-primitive datatype to call ArrayList.remove (Object ) https://code.sololearn.com/cckiG225u83l/?ref=app
29th Mar 2018, 5:27 PM
Sayan Kundu
+ 8
My bad, I thought your code wasn't working. What is your question then?
29th Mar 2018, 5:37 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 6
Convert int to Integer in following way: list.remove (new Integer(2));
29th Mar 2018, 5:16 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 5
Shamima Yasmin, Jacob Marley thanks a lot. I updated that code.
29th Mar 2018, 6:57 PM
Sayan Kundu
+ 4
If I recall correctly, '.remove' is based upon the index and isn't searching for the int inside of the list, which I think that's what they're asking about. Either way, you can easily create the function yourself and use it anytime you want in future code. I threw one together really fast for you and will post it below. It'll allow you to search your ArrayList for a value and either remove just the first occurrence of it or remove all occurrences of it. Let me know if this isn't what you were asking and I can write up new code if needed. https://code.sololearn.com/cgJfTC4MflrJ/#java import java.util.*; public class Program { public static void SearchAndDestroy(int searchFor, ArrayList myList, boolean removeAll){ if(removeAll == true){ for(int i = 0; i < myList.size(); ++i){ if(myList.get(i).equals(searchFor)){ myList.remove(i); // Remove element if it matches. Will remove all. } } } else { for(int i = 0; i < myList.size(); ++i){ if(myList.get(i).equals(searchFor)){ myList.remove(i); // Remove first occurence and break loop. break; } } } } public static void main(String[] args) { ArrayList <Integer> list= new ArrayList<Integer>(); // Add some items to the list list.add(2); list.add(4); list.add(2); list.add(3); list.add(2); list.add(6); list.add(2); System.out.println(list); // Lets test out our function SearchAndDestroy(2, list, false); // Remove first occurrence of 2 only System.out.println(list); SearchAndDestroy(2, list, true); // Remove all occurences of 2. System.out.println(list); } } ::: OUTPUT ::: [2, 4, 2, 3, 2, 6, 2] [4, 2, 3, 2, 6, 2] [4, 3, 6]
29th Mar 2018, 5:35 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
Btw, if that's not what you're asking, you can use something similar to store a list of the indexes associated with whatever you're searching for. So I could have had it store a reference to each occurrence of int 2 in the list, and then from there you can do what you want with that information, such as remove the first two, last two, the middle one, all of them, every other one, etc....
29th Mar 2018, 5:37 PM
Fata1 Err0r
Fata1 Err0r - avatar