Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: Java arrays

  1. #1
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java arrays

    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.

  • #2
    New Coder
    Join Date
    Nov 2004
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Location
    England
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Would this means that arrays are immutable?

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #5
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KeZZeR
    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:
    Code:
     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.

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Location
    Maryland
    Posts
    468
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roy Sinclair
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •