开发者

How does ArrayList work?

开发者 https://www.devze.com 2023-01-11 12:14 出处:网络
What data structure开发者_JAVA技巧 does an ArrayList use internally?Internally an ArrayList uses an Object[].

What data structure开发者_JAVA技巧 does an ArrayList use internally?


Internally an ArrayList uses an Object[].

As you add items to an ArrayList, the list checks to see if the backing array has room left. If there is room, the new item is just added at the next empty space. If there is not room, a new, larger, array is created, and the old array is copied into the new one.

Now, there is more room left, and the new element is added in the next empty space.

Since people really like the source code:

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer.
 */
private transient Object[] elementData;

Straight out of the JDK.


It uses an Object[], and makes a bigger array when the array gets full.

You can read the source code here.


ArrayList uses an Array of Object to store the data internally.

When you initialize an ArrayList, an array of size 10 (default capacity) is created and an element added to the ArrayList is actually added to this array. 10 is the default size and it can be passed as a parameter while initializing the ArrayList.

When adding a new element, if the array is full, then a new array of 50% more the initial size is created and the last array is copied to this new array so that now there are empty spaces for the new element to be added.

Since the underlying data-structure used is an array, it is fairly easy to add a new element to the ArrayList as it is added to the end of the list. When an element is to be added anywhere else, say the beginning, then all the elements shall have to move one position to the right to create an empty space at the beginning for the new element to be added. This process is time-consuming (linear-time). But the Advantage of ArrayList is that retrieving an element at any position is very fast (constant-time), as underlying it is simply using an array of objects.


ArrayList has the basic data structure:

private transient Object[] elementData;

When we actually create an ArrayList the following piece of code is executed:

 this.elementData = new Object[initial capacity];

ArrayList can be created in the two ways mentioned below:

  1. List list = new ArrayList();

The default constructor is invoked and will internally create an array of Object with default size 10.

  1. List list = new ArrayList(5);

When we create an ArrayList in this way, constructor with an integer argument is invoked and create an array of Object with default size 5.

Inside the add method there is check whether the current size of filled elements is greater/equal to the maximum size of the ArrayList then it will create new ArrayList with the size new arraylist = (current arraylist*3/2)+1 and copy the data from old to new array list.


It uses an array, and a couple of integers to indicate the first value - last value index

private transient int firstIndex;

private transient int lastIndex;

private transient E[] array;

Here's an example implementation.


Typically, structures like ArrayLists are implemented by a good old fashioned array defined within the class and not directly accessible outside the class.

A certain amount of space is initially allocated for the list, and when you add an element that exceeds the size of the array, the array will be reinitialized with a new capacity (which is typically some multiple of the current size, so the framework isn't constantly re-allocating arrays with each new entry added).


The Java platform source code is freely available. Here's an extract:

public class ArrayList<E> extends AbstractList<E>
  implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
  /**
   * The array buffer into which the elements of the ArrayList are stored.
   * The capacity of the ArrayList is the length of this array buffer.
   */
  private transient E[] elementData;
  .
  .
  .
}


ArrayLists use arrays to hold the data. Once the number of elements exceeds the allocated array it copies the data to another array, probably double the size.

A (minor) performance hit is taken when copying the array, it's therefore possible to set the size of the internal array in the constructor of the array list.

Furthermore it implements java.util.Collection and and java.util.list, and it's is therefore possible to get the element at a specified index, and iterable (just like an array).


It uses an Object[]. When the array is full it creates a new array which is 50% bigger in size and copies current elements to new array. It happens automatically.


as i understand is that

ArrayList class implements List interface and (as interface only extends other interface) List interface extends Collection interface. while talking about arraylist when we initialize in memory it reserve by default space 10 > and create array of Integer which you normally use. when this this array is full then the another array of interger is created which is greater then default size.

List<Integer> list = new ArrayList<>();

now in memory as => Integer[] list = new Integer[10];

now suppose that you enter 1,2,3,4,5,6,7,8,9,10 array is full now and what happen when you enter 11 in the memory another array of Integer is created which is greater then by default and all element in old array is copied in new array. Internally arraylist user Object[] array.

thats how arrayList work


Basic Data Structure which is used in an ArrayList is –

private transient Object[] elementData;

So, going by declaration it’s an array of Object. When we actually create an arrayList following piece of code is executed –

this.elementData=new Object[initial capacity];

When we create an ArrayList, default constructor is invoked and will internally create an array of Object with default size, which is 10. Now, As we all know that unlike normal arrays, the size of the ArrayList grows dynamically. But how its size grows internally?

So what happens internally is a new Array is created with size 1.5 x currentSize and the data from old Array is copied into this new Array. Every time when it reaches ArrayList at maximum capacity then copy and destroy previous array make new array with new capacity (1.5 x old capacity).

For more details your can read my blog here.

0

精彩评论

暂无评论...
验证码 换一张
取 消