|
-
Jun 1st, 2002, 06:21 AM
#1
Thread Starter
Hyperactive Member
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!
-
Jun 1st, 2002, 08:55 AM
#2
Frenzied Member
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.
-
Jun 1st, 2002, 09:05 AM
#3
Thread Starter
Hyperactive Member
how can I use an array list?
example please
thx!
-
Jun 1st, 2002, 09:20 AM
#4
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);
-
Jun 1st, 2002, 09:23 AM
#5
Frenzied Member
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.
-
Jun 1st, 2002, 02:05 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|