aspose file tools*
The moose likes Beginning Java and the fly likes In the API definition Of ArrayList what does E mean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "In the API definition Of ArrayList what does E mean" Watch "In the API definition Of ArrayList what does E mean" New topic
Author

In the API definition Of ArrayList what does E mean

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
In the API definition Of ArrayList what does <E> mean
I could not find any description of it.

java.util
Class ArrayList<E>

Further

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.





Whais meant by
This may reduce the amount of incremental reallocation.



So this ensureCapacity operation can be used to increase the number of elements an ArrayList can hold?


Varuna


Varuna Seneviratna
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
E represents an Element, which could be any class. For example, if you're building an array list of Integers then you'd initialize it as -



You replace "Integer" with the class that the list is of.

ensureCapacity is used to ensure that the list has enough capacity to take in the new elements. It's called internally every time you add a new item to the list. As the name suggests, ArrayList uses an Array to store the items. So when the array is initialized, it's given an arbitrary length, say 10. Now once you've added 10 items, if you go to add the 11th item, it'll crash because it exceeds the arrays capacity. Hence, ensureCapacity is called (internally) to ensure that there's enough space. So if you were adding the 11th element, the array size might be, say, doubled, to 20.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

And if you know you are going to add 1000 elements, you can call ensureCapacity(1000). If you don't the array size may grow in steps, to (for example) 20, 40, 80, 160, 320, 640, 1280. That's 7 resizes of the array. If you call ensureCapacity(1000) directly, it's only one resize.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
To the Java� Tutorials! Lots about <E> there.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In the API definition Of ArrayList what does E mean