Michael Jackson
Michael Jackson

Reputation: 376

How many values can an ArrayList hold?

I'm new to Java and I would like to know how many values can a arraylist hold.

I'm building an android project that takes values from a database table (I'm printing the values using json and calling the file in java), and records this values on a arraylist.

If my table has like 8000 records, would it be possible to the arraylist hold all these records?

Thank you.

Upvotes: 4

Views: 4946

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726799

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. You could still run out of memory if each individual record takes a significant amount of memory, but it is going to happen before you hit the limit on the number of ArrayList entries.

Upvotes: 5

Todd
Todd

Reputation: 31710

If you look at the documentation for ArrayList, you'll see that the methods that deal with size (adding, creating, determining size, etc) all return an int. This means you have 2^31-1 as a possible max size. However, if you are storing something serious in there, you'll probably run out of memory long before you run out of integers. Depending on memory, 8000 elements is easily within the capabilities of the class (at least with reference to storage space).

Upvotes: 4

Related Questions