jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes about arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about arrays" Watch "about arrays" New topic

about arrays

krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
how many number of elements can a array contain.Is there any limit and does it vary according to type of data.please provide a link if possible.

Thank u
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
I don't think there is a preset limit. The limit varies by machine and is dependant on available memory. If each element takes up a lot of memory, it will mean less room of course.

Essentially it is not something you will probably ever need to worry about.

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
And think about the difference between the memory the array takes up and the memory the objects in the array take up.

LargeObject[] array = new LargeObject[ONE_MILLION]

The memory used by that is one million references or pointers which are only a few bytes each. Every one of them is null and points to nothing. That might well succeed without blowing up your memory.

But you might run out of memory putting the first large object into the array ... or maybe after 1000 or 5000 ...

array[0] = new LargeObject();

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
More precisely, the index of an array should be an integer. So, even assuming you have tons and tons of memory available, the maximum elements you can store into an array should be less than or equal to the Integer.MAX_VALUE.

To test this, try to compile the following piece of code

Quaerendo Invenietis
I agree. Here's the link: http://aspose.com/file-tools
subject: about arrays
jQuery in Action, 3rd edition