• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Size - question clarification  RSS feed

 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick question guys I am using an array list as follows:



As it didnt loop round the above code 49 times as I expected I done this:



The message above printed out that the ImageIconArrayList size was 0. I thought with me specifying 49 above that the list would be of size 49, similarly as to when we declare an array with 49 values the .length method returns 49. 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, if so what is the purpose of adding the 49 like I have done so above.

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?

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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check the API on the ArrayList, you'll find the following:

public ArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

public int size()public int size()
Returns the number of elements in this list.

So you have something with a CAPACITY to hold 49 objects, but since there are none in there, the SIZE is 0.

Why would you want to iterate through something that is empty? This saves you from having to do null checks on every iteration.
 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response, the reason I was wanting to iterate through was because I was going to add 49 objects to the list. I know I could simply have used the number 49, which is what I have now done, but as I am learning I just wanted to ask those questions so I have a better understanding of lists. I assume if I try to add more than 49 objects to the list I will get an error.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David James Thompson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, thanks for the replies, very clear and easy to understand responses. Appreciate it, thanks fred rosenberger and Joanne Neal
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!