Your misunderstanding is here:
uns=new ArrayList(10);
This creates a list whose capacity is 10 ... but that initially contains zero elements.
Then you call this:
uns.add(i,u)
and we can infer from the exception message that i
is 1
. Of course, 1
is outside of the current indexable range of the list, and so you get that exception.
If your intent is that uns
should be initialized with 10 elements, then you need to "fill" the array after creating it; e.g.
for (int ii = 0; ii < 10; ii++) {
uns.add(someValue);
}
(There are 3rd party library methods that will create a list filled with with a given value. But for something this simple I wouldn't go hunting ...)