How are ArrayLists implemented in Java?

It is an array of Object. From the source: hg.openjdk.java.net/jdk7/jdk7-gate/jdk/f... private transient Object elementData.

Yes, the underlying Object is managed as you first surmised, including array growth by doubling. No optimizations for membership testing; just straight searching through the list elements, one by one. It's worthwhile to look at the source code linked by TofuBeer… you can learn a lot by studying the formality, optimization, and "defensive coding" of Sun's/Oracle's engineers.

One should just not forget that there may be one reason or the other why something is implemented in one particular way. Especially all classes that were retrofitted with generics may be implemented in a rather convoluted or "suboptimal" manner for backcomp reasons. – Voo Sep 12 at 3:16.

An ArrayList extends AbstractList and implements four interfaces viz. List, RandomAccess, Cloneable, java.io.Serializable. And it stores elements in an Object array as: private transient Object elementData; If you say: ArrayList arr=new ArrayList(); then by default it creates an ArrayList of size 10.

There is a method private void grow(int minCapacity) which resizes the ArrayList.

Java 1.5 has generics implemented with high overhead, requiring implicit casts both on the way into the collection and on the way out. The compiler automatically casts all objects going into your collection to Dog (for example) and will automatically cast them back to Dog on the way out. Inside the collection, they are treated as generic Objects.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions