Question about java list delete

The public boolean remove(Object o) List method removes an object from the list, but does not move the following items. Just removes the value of the object.
IMHO is a non-intuitive design because the size of the list before and after deletion remains unchanged.
Is there an elegant way to get a list with moved items?

thanks

+2
source share
6 answers

No, that’s not what he does. The item is deleted and all indices following it are reduced by one. What makes you think it acts differently?

+4
source

According to the Java API here , this suggests that the remove function from the DOES shift list

Deletes an item at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one of their indices). Returns the item that has been removed from the list.

EDIT:

Main class:

 import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { ArrayList<A> x = new ArrayList<A>(); A one = new A("one"); A two = new A("two"); A three = new A("three"); A four = new A("four"); A five = new A("five"); A six = new A("six"); A seven = new A("seven"); A eight = new A("eight"); A nine = new A("nine"); A ten = new A("ten"); x.add(one); x.add(two); x.add(three); x.add(four); x.add(five); x.add(six); x.add(seven); x.add(eight); x.add(nine); x.add(ten); for(A item:x){ System.out.println(item.getStr()); } x.remove(four); Iterator<A> i = x.iterator(); while(i.hasNext()){ A item = i.next(); System.out.println(item.getStr()); } } } 

Grade A:

 public class A { private String str; public A(String x){ this.str = x; } public String getStr(){ return this.str; } } 

works great! null pointer exception. Here is how it should be done. the first For loop is an alternative syntax for what I did with the Iterator object. In fact, Java automatically translates the first loop into what looks like a while loop.

+3
source

If you look at an ArrayList to remove an implementation , it uses the local fastRemove (index) method as follows: -

/ * * A private delete method that skips border checking and does not * return a value. * /

 private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work } 

It uses arraycopy, which is proof that you get the whole new list of fresh objects, not the zero filled in between. Is this evidence?

+2
source

The contract for java.util.List implies that a call to remove will decrease the size() value. If you are talking specifically about java.util.ArrayList , then you may be right that the internal array does not move its elements, but it is an implementation detail that should not matter to you in 99% of all cases. If this still matters, then you are trying to optimize for a specific situation, and you should probably implement your own List or use something like java.util.LinkedList .

+1
source

Either your observation is erroneous, or you are using some other kind of implementation of List (and not ArrayList), which does not move the elements to the right of the deleted element. Can you post your code?

If you look at the source code of java.util.ArrayList in JDK8, you will see that the remove (Object o) method effectively copies the elements to the right of the element to be deleted, into the same array, starting from the index of the element to be deleted. Take a look at the source code of ArrayList for more information:

+1
source

If you only need an array of data, just call toArray() .

0
source

All Articles