There are 4 different ways to iterate through a list:
1. With for loop
2. Using advanced for loop
3. using iterator or list iterator
4. using do-while loop
Difference between String,StringBuilder and String Buffer
Thread, Its Life Cycle and nultithreading in java
Using for Loop
The basic loop can be used to iterate through a list
Example : Suppose we have a list of car which includes name of cars .Names can be retrieved with the help of for loop
package test;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class MainS {
public static void main(String[] args) {
List carNames= new ArrayList();
carNames.add("skoda");
carNames.add("bmw");
carNames.add("jaguar");
for (int i = 0; i < carNames.size(); i++) {
System.out.println(carNames.get(i));
}
}
See :How to sort an Array List without using Comparator or sort method
Output :
Using Iterator :
using do-while loop :
See also :
How to reverse a string in java
Difference between Arrays.sort() and collections.sort()
Custom Exception in Java -Tutorial
5 main differences between HashTable and HashMap
Difference between String,StringBuilder and String Buffer
Static varibale,static class,Static method
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class MainS {
public static void main(String[] args) {
List carNames= new ArrayList();
carNames.add("skoda");
carNames.add("bmw");
carNames.add("jaguar");
for (int i = 0; i < carNames.size(); i++) {
System.out.println(carNames.get(i));
}
}
See :How to sort an Array List without using Comparator or sort method
2. Using advanced for loop
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args) {
List carNames= new ArrayList();
carNames.add("skoda");
carNames.add("bmw");
carNames.add("jaguar");
for(Object l :carNames)
{
System.out.println(l);
}
}
}
Working :
for(Object l :carNames)
{
System.out.println(l);
}
Here we took List carNames= new ArrayList();
CarNames list is not of any specific datatype means String ,Integer etc so its storing objects with value skoda,bmw etc.
So while reteriving we used for(Object l :carNames)to retrieve elements.
If you use for(String l :carNames)then it will compilation error because carNames list contains object elements not of String or any other datatype.
If you want to retrieve using a datatype then you must need to specify datatype while declaring the list as
List<String> carNames= new ArrayList<String> ();
OR
List<Integer> carNames= new ArrayList<Integer> ();
OR
ArrayList<String> carNames= new ArrayList<String> ();
OR
Whatever you want :D
Then you can retrieve elements as
for(String l :carNames)
OR
for(Integer l :carNames)
This is very normal …. I want to do something very cool like want to store all type of data objects in one list not a single Generic type.
Like I want to store Integer ,String, Object of a class in one list … yeahhh J
Following is the example for the same :
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args) {
List<Object> carNames= new ArrayList<Object>();
/* Or List carNames= new ArrayList();
carNames.add(new Integer(1));
carNames.add(new String("Hello"));
carNames.add("jaguar");
carNames.add(new Double(10.5));
for(Object l :carNames)
{
System.out.println(l);
}
}
}
That’s coolll !!!!
Output :
Using Iterator :
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args) {
List<Object> carNames= new ArrayList<Object>();
/* Or List carNames= new ArrayList();
carNames.add(new Integer(1));
carNames.add(new String("Hello"));
carNames.add("jaguar");
carNames.add(new Double(10.5));
Iterator i= carNames.iterator;
Iterator i= carNames.iterator;
while(i.hasnext())
{
System.out.println(i.next());
}
}
}
using do-while loop :
package test;
import java.util.ArrayList;
import java.util.List;
public class testing {
public static void main(String[] args) {
List<Object> carNames= new ArrayList<Object>();
/* Or List carNames= new ArrayList();
carNames.add(new Integer(1));
carNames.add(new String("Hello"));
carNames.add("jaguar");
carNames.add(new Double(10.5));
Iterator i= carNames.iterator;
Iterator i= carNames.iterator;
do
{
System.out.println(i.next());
} while(i.hasnext())
}
}
See also :
How to reverse a string in java
Difference between Arrays.sort() and collections.sort()
Custom Exception in Java -Tutorial
5 main differences between HashTable and HashMap
Difference between String,StringBuilder and String Buffer
Static varibale,static class,Static method
nice article :)
ReplyDeletethnxxxx
ReplyDelete