which one is better Generics or not Generics in Java?
I was wondering if I have the following ArrayList with Generics in Java:
int s=0;
ArrayList<Integer> items=new ArrayList();
for (int i=0;i<10;i++){
items.add(i);
}
for (int j=0;j<items.size();j++){
s=s+items.get(j);
}
or is better to use this raw form?
List listitem=new ArrayList();
for (int i=0;i<10;i++){
listitem.add(i);
}
for (int j=0;j<listitem.size();j++){
s=s+(int)listitem.get(j);
}
System.out.println(s);
I mean I have read that when one use Generics arises the problem of the
unboxing and boxing of the variable, that is to change from one primitive
type to an object. So which one would be recommended? and in what case
generics would be an optimal choose?
Thanks
No comments:
Post a Comment