Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about arrays

 
krishna balaji
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic