Home Tutorial Java Iterator Iterator Java Remove

 
 

Iterator Java Remove
Posted on: November 4, 2009 at 12:00 AM
In this section of tutorial we will learn how to use the remove method of the Iterator interface. We will create an example to display the contents of the ArrayList.

  • remove()method removes the last element of the list.
  • This method is used with both Iterator and listIterator.
  • It is used with next() or previous() method.


Example Java Remove Iterator
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class remove {

	public static void main(String[] args) {
		char alphabet = 'a';
		ArrayList list = new ArrayList();
		while (alphabet != 'z') {
			list.add(alphabet++);
		}
		Iterator it = list.iterator();
		while (it.hasNext()) {
			System.out.print(it.next() + " ");
			it.remove();
		}
	}
}

Output

a b c d e f g h i j k l m n o p q r s t u v w x y

Related Tags for Iterator Java Remove:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.