Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capacity of ArrayList [duplicate]

Possible Duplicate:
How to get the capacity of the ArrayList in Java?

How to find the capacity of an ArrayList?

like image 220
user430532 Avatar asked Aug 25 '10 10:08

user430532


People also ask

Can ArrayList hold duplicates?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values. Ordering : ArrayList maintains the order of the object in which they are inserted while HashSet is an unordered collection and doesn't maintain any order.

Does ArrayList have a size limit?

The theoretical limit for ArrayList capacity is Integer. MAX_VALUE, a.k.a. 2^31 - 1, a.k.a. 2,147,483,647.

How many objects can an ArrayList hold?

Since ArrayList in Java is backed by a built-in array, the limit on the size is equal the same as the limit on the size of an array, i.e. 2147483647. Since your project is for android platform, you would run out of memory before reaching this limit. However, 8000 is a relatively small number.


1 Answers

I'm curious, what do you need it for? You should know that the capacity is not (as it may sound) an upper limit of how much you can put into the ArrayList. It's a value representing how much data you can put into the list, without forcing it to reallocate it internal array. Basically, the notion of capacity is only there in order for you to tweak the performance slightly.

Anyway, perhaps you already know that, so here comes the actual answer.

The interface provided by API for ArrayList simply doesn't support such use case. There are many reasons for this. One reason is that you shouldn't care about this. The ArrayList is to be thought of as an unbounded array which abstracts away from details such as capacity.

The closest you can get to controlling the capacity is through the constructor ArrayList(int initialCapacity), and the two methods trimToSize() and ensureCapacity(int minCapacity).

For fun however, I managed to solve it through an ugly reflection-hack (don't use this):

import java.lang.reflect.Field; import java.util.ArrayList; public class Test {      public static void main(String[] args) throws Exception {         ArrayList<Integer> list = new ArrayList<Integer>(3);         for (int i = 0; i < 17; i++) {             list.add(i);             System.out.format("Size: %2d, Capacity: %2d%n",                               list.size(), getCapacity(list));         }     }      static int getCapacity(ArrayList<?> l) throws Exception {         Field dataField = ArrayList.class.getDeclaredField("elementData");         dataField.setAccessible(true);         return ((Object[]) dataField.get(l)).length;     } } 

Output:

Size:  1, Capacity:  3 Size:  2, Capacity:  3 Size:  3, Capacity:  3 Size:  4, Capacity:  5 Size:  5, Capacity:  5 Size:  6, Capacity:  8 Size:  7, Capacity:  8 Size:  8, Capacity:  8 Size:  9, Capacity: 13 Size: 10, Capacity: 13 Size: 11, Capacity: 13 Size: 12, Capacity: 13 Size: 13, Capacity: 13 Size: 14, Capacity: 20 Size: 15, Capacity: 20 Size: 16, Capacity: 20 Size: 17, Capacity: 20 
like image 131
aioobe Avatar answered Oct 07 '22 04:10

aioobe