Here is an example in JS:
var vArray:Vector3[];
var positions:Vector3[];
function AddToArray(){
//Add positions to the vArray.
}
How would I combine two arrays like this? A single array out of two arrays.
Any hint is very much appreciated.
Here is an example in JS:
var vArray:Vector3[];
var positions:Vector3[];
function AddToArray(){
//Add positions to the vArray.
}
How would I combine two arrays like this? A single array out of two arrays.
Any hint is very much appreciated.
You can use System.Array.Copy:
function CombineVector3Arrays (array1 : Vector3[], array2 : Vector3[]) : Vector3[] {
var array3 = new Vector3[array1.length + array2.length];
System.Array.Copy (array1, array3, array1.length);
System.Array.Copy (array2, 0, array3, array1.length, array2.length);
return array3;
}
Arrays can't be resized so you have to create a new array that is big enough to hold the elements of both arrays.
var vArray:Vector3[];
var positions:Vector3[];
function AddToArray(){
var tmpArray = new Vector3[vArray.length + positions.length];
var i = 0;
for (var V : Vector3 in vArray) {
tmpArray[i++] = V;
}
for (var V : Vector3 in positions) {
tmpArray[i++] = V;
}
vArray = tmpArray;
}
But with a List it would be simpler
var vArray:Vector3[];
var positions:Vector3[];
function AddToArray(){
var tmpArray = new List.<Vector3>(vArray);
tmpArray.AddRange(positions);
vArray = tmpArray.ToArray();
}
I'm not at my home computer so can't check whether this exactly right, but it should give you an idea...
var i:int;
for (i=0; i<vArray.Count; i++)
{
vArray _+= positions*;*_
_*}*_
_*```*_
_*<p>If += doesn't work then change the line to:</p>*_
_*```*_
<em><em>vArray <em>= vArray _+ positions*;*_</em></em></em>
<em><em><em>_*```*_</em></em></em>
Thank you. I've tried your first suggestion it I get the this error: NullReferenceException. And your second suggestion will add values. What i'm after is to make a single array out of two.
– anon67157431
Well, it's a bit shorter than my first solution and i guess a bit more efficient than my second ;) Does System.Array.Copy even care about the containing type or is it blind (object) copying?
– Bunny83@Bunny83: http://msdn.microsoft.com/en-us/library/system.arraytypemismatchexception.aspx Aside from being shorter, System.Array.Copy is a little more efficient than doing it yourself.
– Eric5h5