Why use generics
1. Addressing security issues with element storage
2. Solve the problem of type conversion when getting elements
Generics not used
Package org.tizen.test;
Import java.util.ArrayList;
Import java.util.List;
public class Testmain {
public static void Main (String []str)
{
List List = new ArrayList ();
List.add (13);
List.add (14);
Any object and its subclasses that are not using generics can be added to the list
list.add ("Test");
for (int i = 0; I<list.size (); i++)
{
Exception in thread "main" java.lang.ClassCastException:java.lang.String cannot is cast to//java.lang.integer
int a = (Integer) list.get (i);
System.out.println (a);
}
}
}
Using generics
list<integer> List = new arraylist<integer> ();
List.add (30);
List.add (100);
List.add ("AA");
for (int i = 0; I<list.size (); i++)
{
int a = List.get (i);
System.out.println (a);
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java DAY14 Generics