squirellplaying
11-18-2004, 11:44 PM
Is there a reason that once an array is constructed more "slots" can't be added to it.
Line from my curriculium.
Once an array has been constructed, the number of slots it has does not change.
This seams like it would limit the usefulness. This requires you to know how many slots you need before you create it, instead of doing array[array.length]=new value. I sure hope I'm not reading this line correctly.
turbowrx
11-19-2004, 03:13 AM
Yes. That is its intended purpose. There are other data structures that can be used if you don't know the size you'll need. Vector and arraylist are a couple of examples. There are advantages to using an array over an arraylist if you know the size you need. An array fills in contiguous spots in memory. An arraylist could be all over memory if it can't find room. This is slower when it comes time to find records. What I've stated is very basic. Read an section in a java textbook about arrays and then about vectors to get more detailed information. Also, you might find it useful to check out the sun website java tutorials. Later.
KeZZeR
11-19-2004, 11:16 AM
Would this means that arrays are immutable?
Roy Sinclair
11-19-2004, 03:22 PM
Think about it for a second. When you declare an array an amount of memory is allocated to contain that array and that memory could be in the midst of a bif block of available memory or it could have been allocated from a block that happened to be an exact fit. In order to alter the number of entries in that array a new block of memory would have to be allocated, the contents of the current block would then have to be copied to the new location and then the old block of memory would have to be released. That's a lot of overhead going on behind the scenes that would have to be built into any such operation, it's just simpler (and safer) to not allow it.
sad69
11-19-2004, 07:05 PM
Would this means that arrays are immutable?
I think immutable means that you cannot change them. But how can this be so if you can do this:
int[] foo = new Array(5);
foo[0] = 3;
foo[1] = 69;
foo[0] = 7;
You've changed the value of an element in the array. So Java arrays are mutable.
Strings in Java, however, are immutable. You can't change a character in a String without creating a new String -- but then you haven't changed a character in the original String, so you haven't mutated it. So String are immutable.
Hope that helps,
Sadiq.
squirellplaying
11-19-2004, 07:48 PM
Think about it for a second. When you declare an array an amount of memory is allocated to contain that array and that memory could be in the midst of a bif block of available memory or it could have been allocated from a block that happened to be an exact fit. In order to alter the number of entries in that array a new block of memory would have to be allocated, the contents of the current block would then have to be copied to the new location and then the old block of memory would have to be released. That's a lot of overhead going on behind the scenes that would have to be built into any such operation, it's just simpler (and safer) to not allow it.
That was the first thought that came to mind, but figured there must be other ways to do it. It all makes sense now. Thanks.
:thumbsup: