
In this article, we use examples to illustrate the new feature known as generics in Java 5.0.
javac -source 1.5 XXX.java |
String in order to use indexOf method.
What a headache!
import java.util.*;
public class Old1 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Orange");
list.add("Apple");
list.add("Pineapple");
list.add("Lemon");
list.add("Pear");
for(int i=0; i < list.size(); i++) {
String s = (String)list.get(i);
if(s.indexOf('n' ) &t;= 0) {
System.out.println(s);
}
}
}
}
|
javac -source 1.5 New1.java. List<String> list = new ArrayList<String>(); to create a List of String.
You can replace String by other class.
import java.util.*;
public class New1 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Orange");
list.add("Apple");
list.add("Pineapple");
list.add("Lemon");
list.add("Pear");
for(int i=0; i < list.size(); i++) {
String s = list.get(i); // no need to type cast
if(s.indexOf('n' ) >= 0) {
System.out.println(s);
}
}
}
}
|
import java.util.*;
public class Old2 {
public static void main(String[] args) {
Map map = new HashMap();
map.put("Amy", new Integer(55));
map.put("Ben", new Integer(68));
map.put("Charles", new Integer(88));
map.put("Dave", new Integer(88));
map.put("Betty", new Integer(100));
double sum = 0;
Collection scores = map.values();// get all the scores
for(Iterator i = scores.iterator(); i.hasNext();) {
sum+=((Integer)i.next()).intValue();
}
System.out.println("Average is: " + sum/scores.size());
}
}
|
Map<String, Integer> map = new HashMap<String, Integer>() is a map with key type String
and value type IntegerCollection<Integer> scores = map.values() : create a Collection of Integer.Iterator i = scores.iterator() : create an iterator of Integers.
sum+=i.next().intValue(): no type casting.
public class New2 {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("Amy", new Integer(55));
map.put("Ben", new Integer(68));
map.put("Charles", new Integer(88));
map.put("Dave", new Integer(88));
map.put("Betty", new Integer(100));
double sum = 0;
Collection<Integer> scores = map.values();// get all the scores
for(Iterator<Integer> i = scores.iterator(); i.hasNext();) {
sum+=i.next().intValue();
}
System.out.println("Average is: " + sum/scores.size());
}
}
|
double to Double class or in the other way around.
import java.util.*;
public class Old3 {
public static void main(String[] args) {
double[] array = {2,5, 7.94, 8.21, 3.14, 2.72};
ArrayList list = new ArrayList();
for(int i = 0; i < array.length; i++) {
list.add(new Double(array[i]));
}
// find the sum of the doubles in the list
double sum=0;
for(int i = 0; i < list.size(); i++) {
double num = ((Double)list.get(i)).doubleValue();
sum+=num;
}
System.out.println("Sum: " + sum);
}
}
|
double and Double are automatically done by compiler.
public class New3 {
public static void main(String[] args) {
double[] array = {2,5, 7.94, 8.21, 3.14, 2.72};
ArrayList<Double> list = new ArrayList<Double>();
for(int i = 0; i < array.length; i++) {
// expect an Object as argument, but automatically changed to Double
list.add(array[i]);
}
// find the sum of the doubles in the list
double sum=0;
for(int i = 0; i < list.size(); i++) {
// returns a Double, but automatically converted to Double
double num = list.get(i);
sum+=num;
}
System.out.println("Sum: " + sum);
}
}
|
import java.util.*;
public class Old4 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("Orange");
list.add("Apple");
list.add("Pineapple");
list.add("Lemon");
list.add("Pear");
for(Iterator i = list.iterator(); i.hasNext();) {
String s = (String)i.next();
if(s.indexOf('n') >= 0) {
System.out.println(s);
}
}
}
}
|
for(String s : list): it means for each String s in the list .....
import java.util.*;
public class New4 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Orange");
list.add("Apple");
list.add("Pineapple");
list.add("Lemon");
list.add("Pear");
for(String s : list) { // read as for each s in list
if(s.indexOf('n') >= 0) {
System.out.println(s);
}
}
}
}
|
public class Old5 {
public static void main(String[] args) {
int[] a= {1,3,5,3,4,2,4};
for(int i=0; i < a.length; i++) {
int x =a[i];
System.out.println(x);
}
}
}
|
public class New5 {
public static void main(String[] args) {
int[] a= {1,3,5,3,4,2,4};
for(int x : a) {
System.out.println(x);
}
}
}
|
New2.java
import java.util.*;
public class New2b {
public static void main(String[] args) {
Map
|