Jimmy Bobbby wrote:I am now assuming the .size() method only returns the size once the ArrayList is populated, ie if it contained 3 values it would return 3
Correct.
Jimmy Bobbby wrote:if so what is the purpose of adding the 49 like I have done so above.
Behind the scenes an ArrayList actually stores its data in an array. Arrays in
Java cannot be resized - once they are created their size is fixed.
If you want to increase the size of an array, you have to create a new array and then copy the content of the first array into the new array. Obviously this takes time.
So if you create an ArrayList and keep adding items to it new arrays will have to be created in the background at certain times.
Therefore if you know roughly how many objects are going to be added to an ArrayList, it's a good idea to set the capacity of the backing array to this size when you create the ArrayList.
This is what the value you passed to the constructor represenst - it is the capacity rather than the size i.e. how many objects it can hold without having to resize the backing array.
Jimmy Bobbby wrote:One other thing im using the netbeans IDE and for the following line:
it gave me a warning basically saying I didnt need the second <ImageIcon>, hence the line should have read
Does it matter what way this line is wrote? if not what is the standard way of writing it?
Thanks.
Pre Java 7 you had to use the first form. The second form was introduced in Java 7 solely as a way to reduce the amount of typing you had to do.
You'd already specified what type of objects the ArrayList could hold in the declaration - so what's the point of repeating it when you create the ArrayList.
Jimmy Bobbby wrote:Finally, what is the difference between ImageIconArrayList.set and ImageIconArrayList.add, is it basically that the set can be applied to a specific index in the list where is the add just appends them? Can both be used to add items to the list.
Thanks.
set can only be used on an index where an object already exists - it is used to change the object at that index.
add is used to add another object to the ArrayList.
set cannot be used to add objects to the ArrayList