generics - How is ArrayList represented internally in Java Collection Framework.? -


i going through lectures of algorithms on coursera robert sedgewick.i bit confused when mr.robert pointed out 1 cannot use generics arrays not allowed. arraylist in collection framework uses arrays internally , generic datatypes allowed.i mean can following:

arraylist<integer> list = new arraylist<integer>(); 

one hack pointed out this:

public class fixedcapacitystack<item>{     private item[] s;     private int n = 0;  public fixedcapacitystack(int capacity) {  s = (item[]) new object[capacity];} //this hack 

he mentioned ugly hack , must avoided , produces warning during compilation.

my question is:

1.) how arraylist internally represent various generics types?

2.) if (assumed) use hack mentioned above why doesn't produce warning when compile program arraylist?

3.) there better way apart cast above?

per source:

1 - arraylist stores items in object[], , casts value when retrieving individual elements. there's @suppresswarnings("unchecked") cast happens.

2 - 2 answers here - first you're not (typically) compiling arraylist, including on classpath rt.jar in jre/jdk. second arraylist uses @suppresswarnings on unchecked conversion object generic type.

3 - other alternative ("better" quite subjective) require class generic type, , use array.newinstance(class clazz, int capacity) create array, described in this question


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -