Copying

The asdk.algorithm.Copying class provides two static functions for copying elements between collections. The primary function, copy, takes a source collection, a destination collection and a start and end index as parameters. The example below demonstrates using this method to combine a CD collection with a DVD collection into a new disc collection.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var cdCollection:asdk.Collection = new asdk.ArrayList();
cdCollection.addItem("Cold Play");
cdCollection.addItem("Pink Floyd");
cdCollection.addItem("Van Halen");

var dvdCollection:asdk.Collection = new asdk.ArrayList();
dvdCollection.addItem("Braveheart");
dvdCollection.addItem("The Count of Monte Cristo");

var discCollection:asdk.Collection = new asdk.ArrayList();
asdk.algorithm.Copying.copy( cdCollection, discCollection, 0, cdCollection.getSize() );
asdk.algorithm.Copying.copy( dvdCollection, discCollection, 0, dvdCollection.getSize() );
trace( "discCollection: " + discCollection );
//displays discCollection: [Cold Play,Pink Floyd,Van Halen,Braveheart,The Count of Monte Cristo] in output window

The code above creates two ArrayList collections and populates each with some CD (lines 1-4) and DVD (lines 6-8) titles. The important lines are 11 and 12 where the contents of cdCollection and dvdCollection are copied into the newly minted discCollection. Note that the copy method does not return a value, but instead copies the values into the discCollection array that is passed in to the method as the destination (2nd) parameter.

The other static function that is surfaced in the Copying class is copyBackward which will copy elements into the destination collection in reverse order. The example below is similar to the previous example except that copyBackward is called. Note here that the resulting collection looks like [Van Halen,Pink Floyd,Cold Play,The Count of Monte Cristo,Braveheart] since both collections were copied in reverse order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var cdCollection:asdk.Collection = new asdk.ArrayList();
cdCollection.addItem("Cold Play");
cdCollection.addItem("Pink Floyd");
cdCollection.addItem("Van Halen");

var dvdCollection:asdk.Collection = new asdk.ArrayList();
dvdCollection.addItem("Braveheart");
dvdCollection.addItem("The Count of Monte Cristo");

var backwardDiscCollection:asdk.Collection = new asdk.ArrayList();
asdk.algorithm.Copying.copyBackward( cdCollection, backwardDiscCollection );
asdk.algorithm.Copying.copyBackward( dvdCollection, backwardDiscCollection );
trace( "backwardDiscCollection: " + backwardDiscCollection );
//displays backwardDiscCollection: [Van Halen,Pink Floyd,Cold Play,The Count of Monte Cristo,Braveheart] in output window