Collection ArrayList Example with ForEach Loop,Iterator,ListIterator
No comments:
Example :
package com.harish.collections;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
al.add(60);
al.add(70);
System.out.println("ArrayList : " + al);
System.out.println("\n");
System.out.println("I am Running ForEach Loop");
for (int itr : al) {
System.out.println("For Loop :" + itr);
}
System.out.println("\n");
System.out.println("I am Running Iterator");
Iterator<Integer> alitr = al.iterator();
while (alitr.hasNext()) {
Integer itrinteger = (Integer) alitr.next();
System.out.println("Iterator : " + itrinteger);
}
System.out.println("\n");
System.out.println("I am Running List Iterator");
ListIterator<Integer> litr = al.listIterator();
while (litr.hasNext()) {
Integer lstitrinteger = (Integer) litr.next();
System.out.println(" List Iterator : " + lstitrinteger);
}
System.out.println("\n");
System.out.println("I am Running List Iterator Previous");
while (litr.hasPrevious()) {
Integer integer = (Integer) litr.previous();
System.out.println(" List Iterator Previous: " + integer);
}
System.out.println("\n");
System.out.println("I am Removing First Element");
al.remove(0);
System.out.println(al);
System.out.println("\n");
System.out.println("I am Adding Element");
al.add(80);
System.out.println(al);
System.out.println("\n");
System.out.println("I am Setting First Element");
al.set(0, 100);
System.out.println(al);
}
}
Related posts
Share this post
0 comments: