View RSS Feed

Advanced Java

Generic List in Java

Rate this Entry
by , 02-18-2012 at 03:43 PM (1530 Views)
It is possible to do the generification of java.util.List. This shows that List instances might be of a specific type. That type of instances in such cases could only be read from List.

Generic List is explained by this code.
Java Code: This is the code to expalin Generic List declaration
List<String> list = new ArrayList<String>;
String instances can be stored by this list.

Exists are being checked just at the compilation time by this generic type.

Accessing a Generic List

To retrieve or add a String to a generic list, use this code.
Java Code: This is the code to retrieve or add a String to a generic list
List<String> list = new ArrayList<String>;
String string = "my string";
list.add(string);
string2 = list.get(0);
Iterating a Generic List

For iteration over a generic list, use this code.
Java Code: This is the code to explain iteration over a generic list
List<String> list = new ArrayList<String>;
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
  String aString = iterator.next();
}

Submit "Generic List in Java" to Facebook Submit "Generic List in Java" to Digg Submit "Generic List in Java" to del.icio.us Submit "Generic List in Java" to StumbleUpon Submit "Generic List in Java" to Google

Tags: generic list Add / Edit Tags
Categories
Generics

Comments