Below examples include ArrayList and Iterator including some code/example for Array also since all these are closely related and look similar.
package newPkg;
import
java.util.ArrayList;
import
java.util.Iterator;
public class NewClass {
public static void main(String[] args) {
String[]
stArray = new String[6];
int[] intArray = new int[7];
ArrayList
arList1 = new
ArrayList(); //or
ArrayList
arList2 = new
ArrayList<>(); //or
ArrayList
arList4 = new
ArrayList<>(); //or
ArrayList
arList3 = new ArrayList();
// List arList4 =
{"A","B","C","D"};
stArray[0] = "Ram";
intArray[0] = 100;
arList1.add("Sita");
stArray[1] = "Shyam";
intArray[1] = 200;
arList1.add(1,"Radha"); //with index
of the data
stArray[2] = "USA";
intArray[2] = 300;
arList2.add("DC");
stArray[3] = "Canada";
intArray[3] = 400;
arList2.add(1,"NY");//with index
of the data
arList2.add(2,"Toranto");//with index
of the data
arList3.add(5);
arList3.add(1,"Africa");
arList3.add(1);
arList3.add("11");
arList4.add(11);
arList4.add(12);
arList4.add(13);
arList4.add(14);
//itrating
over an array
//use for loop
for (int i=0;i<stArray.length; i++){
String
x = stArray[i];
System.out.println("x =
"+x);
}
//or
for (String j:stArray){
String
y = j;
System.out.println("y =
"+y);
}
Iterator
itr1 = arList1.iterator();
System.out.println("itr1 =
"+itr1);
Iterator
itr2 = arList2.iterator();
System.out.println("itr2 =
"+itr2);
Iterator
itr3 = arList3.iterator();
System.out.println("itr3 =
"+itr3);
Iterator
itr4 = arList4.iterator();
System.out.println("itr4 =
"+itr4);
//finding
values of array list , three ways :
for(int i=0;i<arList3.size();i++){
System.out.println("arList3Values
= "+arList3.get(i));
}
//or
for (Integer arList4Values : arList4) {
System.out.println("arList4Values
= "+arList4Values);
}
//or
System.out.println("list
value 1 = "+itr3.hasNext());
System.out.println("list
value 2 = "+itr3.hasNext());
System.out.println("list
value 3 = "+itr3.hasNext());
if(itr3.hasNext()){
System.out.println("list
value random1 = "+itr3.next());
}
if(itr3.hasNext()){
System.out.println("list
value random2 = "+itr3.next());
}
if(itr3.hasNext()){
System.out.println("list
value random3 = "+itr3.next());
}
if(itr3.hasNext()){
System.out.println("list
value random4 = "+itr3.next());
}
if(itr3.hasNext()){
System.out.println("list
value random5 = "+itr3.next());
}
if(itr3.hasNext()){
System.out.println("list
value random6 = "+itr3.next());
}
//below while is skipped because
itr3.hasNext() is false
while (itr3.hasNext()) {
System.out.println("from
while "+itr3.next());
}
System.out.println("list
value 1 = "+itr3.hasNext());
System.out.println("list
value 2 = "+itr3.hasNext());
System.out.println("list
value 3 = "+itr3.hasNext());
if(itr4.hasNext()){
System.out.println("4th
iteration value random1 = "+itr4.next());
}
if(itr4.hasNext()){
System.out.println("4th
iteration value random2 = "+itr4.next());
}
//below while prints two values only ,
since two are already printed by above statements
while (itr4.hasNext()) {
System.out.println("4th
iteration from while "+itr4.next());
}
}
}
OUTPUT
x = Ram
x = Shyam
x = USA
x = Canada
x = null
x = null
y = Ram
y = Shyam
y = USA
y = Canada
y = null
y = null
itr1 = java.util.ArrayList$Itr@15db9742
itr2 = java.util.ArrayList$Itr@6d06d69c
itr3 = java.util.ArrayList$Itr@7852e922
itr4 = java.util.ArrayList$Itr@4e25154f
arList3Values = 5
arList3Values = Africa
arList3Values = 1
arList3Values = 11
arList4Values = 11
arList4Values = 12
arList4Values = 13
arList4Values = 14
list value 1 = true
list value 2 = true
list value 3 = true
list value random1 = 5
list value random2 = Africa
list value random3 = 1
list value random4 = 11
list value 1 = false
list value 2 = false
list value 3 = false
4th iteration value random1 = 11
4th iteration value random2 = 12
from while 13
from while 14
x = Shyam
x = USA
x = Canada
x = null
x = null
y = Ram
y = Shyam
y = USA
y = Canada
y = null
y = null
itr1 = java.util.ArrayList$Itr@15db9742
itr2 = java.util.ArrayList$Itr@6d06d69c
itr3 = java.util.ArrayList$Itr@7852e922
itr4 = java.util.ArrayList$Itr@4e25154f
arList3Values = 5
arList3Values = Africa
arList3Values = 1
arList3Values = 11
arList4Values = 11
arList4Values = 12
arList4Values = 13
arList4Values = 14
list value 1 = true
list value 2 = true
list value 3 = true
list value random1 = 5
list value random2 = Africa
list value random3 = 1
list value random4 = 11
list value 1 = false
list value 2 = false
list value 3 = false
4th iteration value random1 = 11
4th iteration value random2 = 12
from while 13
from while 14
No comments:
Post a Comment