Results 1 to 6 of 6

Thread: expanding arrays without data loss

  1. #1

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347

    Question expanding arrays without data loss

    Code:
    Word = new GlobalData.Word[WordCount + 1];
    				Word[WordCount] = new GlobalData.Word();
    				Word[WordCount].Name = sInterop;
    				WordCount += 1;
    this works fine but there's a little problem left:

    Code:
    Word = new GlobalData.Word[WordCount + 1];
    this renews the array size + 1 but also erases all data saved
    in the existing array before.

    is there any whay to save the existing information?

    thx!

  2. #2
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Hey vbzero..

    I don't know a way to save it automaticly, but maybe put it in a tmp array first and than copy it over to the resized one?

    Another idea is to use a ArrayList
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  3. #3

    Thread Starter
    Hyperactive Member vbzero's Avatar
    Join Date
    Aug 2000
    Location
    Vienna
    Posts
    347
    how can I use an array list?

    example please

    thx!

  4. #4
    hellswraith
    Guest
    Here is how to upsize an array by one without data loss. You will need a temp array declared to hold the information while you upsize the real array.

    Code:
    // Up the size of the array by one.
    ArrayTemp = new String[ArrayMain.Length];
    ArrayMain.CopyTo(ArrayTemp,0);
    ArrayMain = new String[ArrayTemp.Length + 1];
    ArrayTemp.CopyTo(ArrayMain,0);

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Yes do what Jop said. Use an ArrayList

    Here is a short example.

    Code:
    ArrayList aL = new ArrayList();
    
    Word = new GlobalData.Word();
    Word.Name = sInterop;
    
    aL.Add(Word);
    Look up ArrayList in the help docs.

  6. #6
    hellswraith
    Guest
    It probably doesn't matter 99% of the time, but they use memory that you aren't using. By that, I mean that when you create an arraylist, it's default size is 16. If you only add in 3 items, you are wasting a little bit of memory space. When you add the 17th item, it will then expand the size to 32. That means you are wasting 16 elements worth of memory. There is also the tiny bit of overhead calling the add method and remove methods. Again, this little bit of overhead in the grand scheme of things is nothing, most of the time, but if you are all about using as little memory as possible, you should stick with regular arrays. I don't know why, but I felt like getting deeper into the subject. I am now off to use an arraylist for my program...lol.

Posting Permissions

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



Click Here to Expand Forum to Full Width