VBForums >
.NET >
C# > [RESOLVED] Changing The Size Of An Array Without Losing The Data?
Click to See Complete Forum and Search --> : [RESOLVED] Changing The Size Of An Array Without Losing The Data?
2MuchRiceMakesMeSick
Jan 30th, 2006, 08:51 AM
Is there a way to resize an array with losing the data in the array.
I need to resize as the data comes and making the array bigger then it will ever hold will not work.
Any ideas?
DeadEyes
Jan 30th, 2006, 09:08 AM
you should use an ArrayList.
mendhak
Jan 30th, 2006, 09:13 AM
public static Array ReDimAndPreserve(Array inputArray, int newsize) {
Array result=(Array)Activator.CreateInstance(inputArray.GetType(), new object[] {newsize});
Array.Copy(inputArray, result, Math.Min(inputArray.Length, result.Length));
return result;
}
I called it RedimAndPreserve because of an equivalent function in VB6/VB.NET which does this.
jmcilhinney
Jan 30th, 2006, 05:39 PM
You probably should use a collection for this, but note that if you do use mendhak's method then don't just increase the size of the array by one each time. You should do something like what collections do with their Capacity property. With an ArrayList the Capacity defaults to 16 unless you specify otherwise. When you try to add an item that goes beyond that capacity the collection allocates enough memory to double the capacity. You should do something similar with your array if you use one. If not doubling it each time at least increasing it by a block rather than a single element at a time. You should also note that the ArrayList has a ToArray method, so you can use an ArrayList and still get an array once you're finished if you specifically need an array for some reason.
2MuchRiceMakesMeSick
Jan 31st, 2006, 03:52 AM
Thanks guys. I appreciate the help :D
wossname
Jan 31st, 2006, 06:54 AM
#1 rule in optimising:
- Don't use collections when you can get away with arrays.
2MuchRiceMakesMeSick
Feb 1st, 2006, 05:13 AM
They are alot slower?
mendhak
Feb 3rd, 2006, 12:13 PM
With ease and maintenance (collections), you make certain sacrifices along the way.
jmcilhinney
Feb 3rd, 2006, 08:14 PM
In your average GUI app you would not notice the difference between using an array and a collection. Even using ReDim Preserve on an array repeatedly probably wouldn't make a noticable difference unless the array was very big. That certainly doesn't mean that should not try to write efficient code. It is a fact that using a collection is easier because you don't have to think about the size issue. If you know the size you need or can make a pretty good guess then using an array definitely would be preferable, particularly for value types, which would need to be boxed in a collection. I'm not sure how many times you would have to resize an array before a collection would become more efficient.
jmcilhinney
Feb 4th, 2006, 08:36 PM
I just realised that I was using VB.NET terminology in the C# forum. A point of interst for users of .NET 2.0 is that the Array class now has a Resize method that works just like ReDim Preserve in VB.NET.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.