Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In case of an arraylist in Java. What will the maximum capacity of the list

Tags:

java

Now as per SCJP the arraylist max. size should be depending on the size of available memory, but list.getSize() returns an Integer. So is it safe to assume that INTEGER.MAXSIZE is the max. capacity of array . i.e int Max allowed value

like image 982
SanketRao Avatar asked Mar 15 '23 07:03

SanketRao


2 Answers

Well, since ArrayList is backed by an array, its max capacity can't be higher than the max length of an array, which is bound by Integer.MAX_VALUE (since the index of an array is always an int).

like image 99
Eran Avatar answered Apr 06 '23 04:04

Eran


There is another question with an answer matching your question and also an simple example to test: Do Java arrays have a maximum size?.

There are two limits:

1.- The available memory

2.- The max size of an Integer

But im pretty sure you hit the first before the second limit.

Hope that helps!

like image 36
Felix Gerber Avatar answered Apr 06 '23 06:04

Felix Gerber