|
-
Nov 3rd, 2005, 03:02 PM
#1
Thread Starter
Addicted Member
[Resolved] - Duplicate Int[]
I want to duplicate an array of int[] into another one.
(If the array is NULL, then have the new array be NULL, if it contain X elements, then have the new array contain X elements also)
Is there a build in function to do that?
I've try copyTo and Clone without success.
Thks
Last edited by Megistal; Nov 3rd, 2005 at 04:06 PM.
-
Nov 3rd, 2005, 04:06 PM
#2
Thread Starter
Addicted Member
Re: Duplicate Int[]
pfffff instead of looking around for hours I've made my own function to do it.
VB Code:
private int[] copierTableauInt (int[] intLeTableau) {
if (intLeTableau == null) return (null); // Tableau Non-instancié
int[] intretTableau = new int[intLeTableau.Length];
// itérations sur tous les éléments int du tableau source
for (int i = 0; i < intLeTableau.Length; ++i) {
intretTableau[i] = intLeTableau[i];
}
return (intretTableau);
}
-
Nov 3rd, 2005, 06:47 PM
#3
Re: [Resolved] - Duplicate Int[]
Why would you not just use CopyTo? Just create another array of the same length and type and then call:
Code:
arr1.CopyTo(arr2, 0)
-
Nov 3rd, 2005, 09:54 PM
#4
Thread Starter
Addicted Member
Re: [Resolved] - Duplicate Int[]
That's the problem, I don't know in advance what will be the size of the source array. And it could be a null one. So using myArray.Length to create a new array could end up with an error.
-
Nov 3rd, 2005, 10:00 PM
#5
Re: [Resolved] - Duplicate Int[]
Code:
int[] arr1;
int[] arr2;
if (arr1 == null)
{
arr2 = null;
}
else
{
arr2 = Array.CreatInstance(typeOf(Int32), arr1.Length);
arr1.CopyTo(arr2, 0);
}
-
Nov 3rd, 2005, 10:04 PM
#6
Thread Starter
Addicted Member
Re: [Resolved] - Duplicate Int[]
And if I was to use if to detect the presence of null array each time I need to copy an array it would be a very unbeautiful code. Now I have only one line of code to copy an entire array. Not quite rapid but it doesn't matter for this kind of project.
-
Nov 3rd, 2005, 10:07 PM
#7
Thread Starter
Addicted Member
Re: [Resolved] - Duplicate Int[]
Aahahha you post what I was writing to avoid at that time!
That's definitely what I don't want. I must copy quite a few array. I want also to reduce redundancy of my code.
-
Nov 3rd, 2005, 10:15 PM
#8
Re: [Resolved] - Duplicate Int[]
OK I missed that bit earlier, but you should still use that code or something similar in a function of your own that you can then call from anywhere.
-
Nov 4th, 2005, 11:16 AM
#9
Thread Starter
Addicted Member
Re: [Resolved] - Duplicate Int[]
I guess the copyTo don't create the array, it must already be created. I prefer using built-in code which often result in a more efficient, more beautiful and a less error prone code.
Aahahha you post what I was writing to avoid at that time!
Just a coincidence that both of use wrote a reply at the same time and not knowing that the other was doing the same. This remind me kind of bug you can have with a dabatase ehhehe
-
Nov 4th, 2005, 05:08 PM
#10
Re: [Resolved] - Duplicate Int[]
Well, there is also the Clone method, but that's an instance member so you can't use it if your original array is a null reference. You could do this though I guess:
Code:
int[] newArray = (oldArray == null ? null : oldArray.Clone());
-
Nov 4th, 2005, 05:16 PM
#11
Thread Starter
Addicted Member
Re: [Resolved] - Duplicate Int[]
Yes it's becoming more and more elegant 
I didn't though of that one, pretty nice
int[] newArray = (oldArray == null ? null : oldArray.Clone());
-
Nov 4th, 2005, 05:20 PM
#12
Re: [Resolved] - Duplicate Int[]
You would probably have to cast the return value of Clone as int[] also.
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
|