Generics

Last Modified On : 21-Aug-2004


// Generics
import java.util.*;
class Student
{
   private String name,course;
   public Student(String name, String course)
   {  
        this.name = name ;
        this.course = course;
   }
   public String toString()
   {
      return  name + "-" + course;
   }
}
    

public class Generics
{ 
     public static void main(String ...args)
     {
        ArrayList  al = new ArrayList();         
        al.add("One");
        al.add("Two");
        al.add("Three");

        for ( String s : al)
          System.out.println(s);

        ArrayList  sl = new ArrayList();

        sl.add ( new Student("Jason","Servlets") );
        sl.add ( new Student("Romans","Ejb") );
 
        printStudents(sl);       

        // Following code triggers error
        // printStudents(al);
 
     }

    public static void printStudents(ArrayList sl)
    {
         for ( Student s : sl)
            System.out.println( s.toString());
    }

}