[RESOLVED] Changing The Size Of An Array Without Losing The Data?
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?
Re: Changing The Size Of An Array Without Losing The Data?
you should use an ArrayList.
Re: Changing The Size Of An Array Without Losing The Data?
Code:
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.
Re: Changing The Size Of An Array Without Losing The Data?
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.
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
Thanks guys. I appreciate the help :D
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
#1 rule in optimising:
- Don't use collections when you can get away with arrays.
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
With ease and maintenance (collections), you make certain sacrifices along the way.
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
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.
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
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.