Let's say it's an integer, then persumably that means you can have only 2 to power of 32 elements. Okay, let's say it's a long, then we can have 2 to the power of 64 elements. Agreed? But what this means is that an array is limited in the number of elements it can hold. So the size of an array *is not* limited by the size of the file system. Does anybody know what type the length instance variable of an array is? cheers bob
-------------------------------<br />The early bird gets the worm! So if you want something else for breakfast, stay in bed.<br />-------------------------------
The length member is an int. However, in general, you're probably going to be limited by the amount of memory rather than by the number of indices available. I tried the following code:
and I received an OutOfMemoryError when I tried to execute it. I've got about a half a gig of memory, about half of which was available. I believe you're right, though. You are only allowed to create an array of size Integer.MAX_SIZE. In fact, I tried this code:
and I receieved a NegativeArraySizeException. I hope that helps, Corey Thinking about this some more, think of how big an array can be. That an array with 2147483647 items. If each one is an int, that's 4 * 2147483647 bytes, or 8589934588 bytes. That's about 8589934 KB, which, if you keep going, comes out to be about 8 GB. That's a big array. [ May 28, 2002: Message edited by: Corey McGlone ]
That code doesn't quote show that larger arrays are impossible - the Integer.MAX_VALUE + 1 would evaluate to a negative number even before the compiler tries to use it as an array size. But try this: This yields a more telling compile error: possible loss of precision found : long required: int This indicates the compiler expects the value inside  to be an int. So yes, it's impossible to creat an array with larger size than that in Java. I'm tempted to say how two billion elements ought to be more than anyone could possibly need anyway (in 1-D at least) - but I recall that Bill Gates said something similar about 64 kb of RAM some years ago, and we know how that turned out. :roll:
If you need more then Integer.MAX_VALUE values in an array just start using multi-demensional arrays and index into them. That being said, I believe Corey is right when he says you'll run out of memory before hitting MAX_VALUE entries lance
Joined: Jan 30, 2000
But Thomas, that doesn't mean anything - you get that error just for mentioning a literal like 2147483648 which is too big for an int. You can make this error go away by appending an L to indicate it's really a long - and then you get the same error I cited earlier: possible loss of precision found : long required: int We're all in agreement that an array this big is not possible in Java - I'm just disagreeing with the proof offered. There's actually a specific rule about this in JLS 15.10: The promoted type must be int, or a compile-time error occurs; this means, specifically, that the type of a dimension expression must not be long.
Right. There are two issues. One is that array subscripts can't be wider than an int, so longs are not allowed. The second is that any integer literal is an int, including literal numbers like 2147483648 (which is actually too big to fit in an int so you get a compiler error.) Integer.MAX_VALUE is defined as an int. If you wanted you could always do (long)Integer.MAX_VALUE, or Long.MAX_VALUE, but then you couldn't use this as an array subscript.
Joined: May 20, 2002
Thanks everybody, this has been both interesting and useful. The discussion raises other questions, like whether vectors are similarly limited (i suspect they are), and how we might get round this limitation. The array of arrays idea mentioned above sounds promising... it leaves me wondering whether multidimensional arrays are actually implemented by dividing up the capacity of a single array or whether they are implemented as an array of arrays. If anybody happens to know the answer to this then do please let me know. thanks again everybody. best wishes email@example.com
Joined: Dec 20, 2001
In Java, an array is really an object. Therefore, a variable of an array type is really a reference variable. This means that the variable really just contains a reference to an array. Similarly, a multi-dimensional array would be an array of references to other arrays. For example, the following would look like this:
In this example, each of the '@' symbols in the diagram would be an Object reference. At this point, each reference would be set to null. I hope that helps, Corey
Joined: Jan 07, 2002
Originally posted by bob towers: Thanks everybody, this has been both interesting and useful. The discussion raises other questions, like whether vectors are similarly limited (i suspect they are), and how we might get round this limitation
Interesting. I've never thought of it really as a limitation. For practical purposes, there's no way you could make use of an array of longs. I mean, how often do you really need to address more than 2 billion+ items in ram at one time? I can see how you might want to cache a large portion of these objects, but to actually need them available at one time, in RAM, is only going to occurr in the most high-end of simulation systems. If you were to just declare an int array of Integer.MAX_VALUE elements , the memory requirements of just the elements would be over 8GB. That's not counting any overhead you might need for the array structure itself. And a single array of int primitives usually isn't all that useful. Say you had a more complex data structure, an array of objects of some arbitrary class type. Your RAM requirements could easily exceed 100GB. Most modern day mainstream computers can't even physically address this amount of memory, let alone have physical space to store that much memory. For a comparison, an int array of Long.MAX_VALUE elements would require 8x10^6 Terrabytes So in everyday practice, this "limitation" on array sizes is a non-issue.