ArrayList removes an element with indices 0 and 1

I would like to remove elements in an ArrayList with index 0 and 1. But it does not work, and I do not know how to do this.

The code is as follows

import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; public class Test{ public static void main(String[] args){ Collection c = new ArrayList(); c.add("A"); c.add("B"); c.add("C"); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); System.out.println(""); c.remove(1); c.remove(0); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); } } 

Output signal

 A B C A B C 

But there must be a way out

 A B C C 
+6
source share
6 answers

I believe this is because you are calling remove (int) on Collection . The collection does not declare a remove (int) method, but has a delete (Object), so java automatically adds your int to Integer. But since this integer is not in the collection, nothing is removed

+6
source

I think you have taken an important lesson.

The problem is that Collection does not support remove(int) , only remove(Object) . Thus, the compiler boxes your int as an Integer, this element is not in the collection, and therefore it does not delete it.

If you declare a collection as an ArrayList, it works.

+2
source

Change c to ArrayList because:

Collection.remove (Object o)

Removes one instance of the specified item from this collection, if present (optional operation). More formally removes an element e such that (o == null? E == null: o.equals (e))

In the above example, if it was c.remove ("A"); he will work. Writing c.remove (1); Searches for the Integer object to be deleted.

ArrayList.remove (int index)

Deletes an item at the specified position in this list. Shifts any subsequent elements to the left (subtracts one of their indices).

So your program should be as follows:

 public class Test{ public static void main(String[] args){ ArrayList c = new ArrayList(); c.add("A"); c.add("B"); c.add("C"); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); System.out.println(""); c.remove(1); c.remove(0); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); } } 
+2
source

As already mentioned by @ControlAltDel, Collection does not support remove(int) only remove(Object) , and int automatically placed in the Integer field and that Integer not in the collection; therefore, nothing is deleted.

If you want to save c as a collection, you can remove the first two elements using Iterator.remove() ; eg:

 public static void main( final String[] args ){ Collection<String> c = new ArrayList<>( Arrays.asList("A","B","C") ); for( String str : c ) System.out.println(str); System.out.println(); Iterator<String> it = c.iterator(); it.next(); it.remove(); it.next(); it.remove(); for( String str : c ) System.out.println(str); } 
+1
source

What do you really need

 c.remove((String)"A"); c.remove((String)"B"); 

instead of accessing them using indexes if you want to continue using Collection . The reason is that the remove method in Collection expects an Object argument. Therefore, when you write c.remove(0) , it searches for a list for element 0 and tries to delete it.

Please consider the performance tradeoff when using Collection instead of List for this case.

+1
source

Collections are just collections of data, so they donโ€™t always keep order. I would recommend replacing the collection with an ArrayList

 public class Test{ public static void main(String[] args){ ArrayList c = new ArrayList(); c.add("A"); c.add("B"); c.add("C"); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); System.out.println(""); c.remove(1); c.remove(0); for(Iterator i = c.iterator(); i.hasNext();) System.out.println(i.next()); } } 
0
source

Source: https://habr.com/ru/post/989903/


All Articles