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.
source share