|
-
Jan 30th, 2006, 09:51 AM
#1
Thread Starter
Lively Member
[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?
-
Jan 30th, 2006, 10:08 AM
#2
Re: Changing The Size Of An Array Without Losing The Data?
you should use an ArrayList.
-
Jan 30th, 2006, 10:13 AM
#3
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.
-
Jan 30th, 2006, 06:39 PM
#4
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.
-
Jan 31st, 2006, 04:52 AM
#5
Thread Starter
Lively Member
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
Thanks guys. I appreciate the help
-
Jan 31st, 2006, 07:54 AM
#6
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.
I don't live here any more.
-
Feb 1st, 2006, 06:13 AM
#7
Thread Starter
Lively Member
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
-
Feb 3rd, 2006, 01:13 PM
#8
Re: [RESOLVED] Changing The Size Of An Array Without Losing The Data?
With ease and maintenance (collections), you make certain sacrifices along the way.
-
Feb 3rd, 2006, 09:14 PM
#9
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.
-
Feb 4th, 2006, 09:36 PM
#10
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.
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
|