As salamo alaikum wa rahmatullah
Example of Array List and Fetch Element using Iterator in Java
package collections.demo;
import java.util.ArrayList;
import java.util.Random;
import java.util.*;
public class ArrayListIterationDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> iList=new ArrayList<Integer>();
for(int i=0; i<=10;i++) {
Random rand=new Random();
int a=rand.nextInt(1000);
iList.add(a);
}
Iterator itr=iList.iterator();
System.out.println("Iterate usin for loop");
for( ; itr.hasNext() ; ) {
System.out.println(itr.next());
}
itr=iList.iterator();
System.out.println("Iterate usin while loop");
while(itr.hasNext()) {
System.out.println(itr.next());
}
}
}
#kazonline #kazacademy
Jazak allah khair
No comments:
Post a Comment