I was working on a generic class, which had to store T type values in a private ArrayList and have the usual get, set methods, but when I initialize the ArrayList it just stays to size 0! I explicitly use the constructor with int argument, but it just stays 0. Here's my constructor and get method (the class is called GenericVector<T> ):
public GenericVector(int n)
{
vector = new ArrayList<>(n);
}
public T get (int pos)
{
if (pos >= vector.size())
{
System.out.println("UR DOIN WRONG");
System.out.println("Size is" + vector.size());
return null;
}
return vector.get(pos);
}
And here's my main:
public static void main(String[] args)
{
GenericVector<String> vec = new GenericVector<String>(5);
vec.set(0, "EEE");
System.out.println("" + vec.get(0));
}
It just prints:
UR DOIN WRONG
Size is 0
null
I don't really get why initializing the vector with new ArrayList<>(n) doesn't work.
size()returns the number of elements in the list. Initializing it with a size only allocates space but does not put anything into the list.nullto the list N times.Tinstead of creating an array list?Tdon't really play well together.