As3 data replication and deep Replication

Source: Internet
Author: User

Copying an array refers to generating a new array with the same content as the original array. Array assignment is frequently used and important.
There are two replication methods: shallow replication and deep replication. This concept is not unique to arrays. There are some differences between the replication mode and the replication mode of reference data.

1. When should I use shortest replication? When should I use deep replication?

When all elements in an array are of the base metadata type, that is, all elements are of the value type, there is no distinction between replication and replication. To copy data, use the shortest COPY method described below.
However, when all the elements in the array are complex data types, that is, when the data type is referenced, there are two options for replication: one is shallow replication and the other is deep replication.
When some elements in the array are the base metadata and some elements are complex data types, you should also consider the replication method. Because actionfscrip
3 arrays are not typed arrays and can be stored differently
Type of data, so this situation is also very common. For elements of the basic metadata type, no matter which method is used for replication, the execution effect is the same. Only elements of complex data types have the difference of shallow replication of the sum of priest.


For example, if array a [1, "string", [, 3], the third element is an array and belongs to a complex type element, the impact of the two replication methods needs to be considered. The other connected elements, a numeric type and a string type, all belong to the base metadata type and do not need to consider the repeat method.

2. Shallow Replication

The method to generate a shortest copy of an array is simple. You only need to call
The Slice Method or Concat method generates a shortest copy of the array without passing any parameters. The usage is as follows.


VaR copy: array = originaarray. Concat (); var copy: array =
Originaarray. Slice ();


Generate with shortest copy
Each element of the new array stores only references. The references stored by elements in the same position in the two arrays point to the same object. Then, if you operate on this element, it is the object pointed to by the Operation Reference. If the object status changes, it will also affect the elements in the corresponding position in another array. Note This.

The following example vividly shows the method of shallow replication and the key points that must be paid attention to when using shallow replication.
Let's explain it first.CodeContent.
There is an array Foo whose first element is
String
Type, which belongs to the value type. Its second element isArray
Type. The third element isObject
Type, all belong to the reference type. Use
The Slice Method (or the Concat method) generates a shortest copy of the foo of the array-array bar. Then, modify the element content of the array bar, and then associate it with the array Foo.
. The result shows that modifying the value type element in the array bar does not affect the corresponding element in Foo. If you change the reference type element in bar, foo
The corresponding elements are also changed. Not only new users, but also veterans often forget this point, so as to break their heads.


VaR objectelement: Object = {name: "kingda", Web: "www.kingda.org
"};
VaR
Arrayelement: array = [1, 2, 3];

VaR FOO: array = ["a string
Element ", arrayelement, objectelement];
Trace (FOO );
// Output Foo content: A String
Element, 1, 2, 3, [object]

// 1. Use the slice method to generate a shallow copy of Foo and assign a bar value
VaR
Bar: array = Foo. Slice ();
// You can also use: Foo. Concat ();

// 2. Compare Foo and bar

Trace (bar );
// Output the content of the shortest copy bar: A string element, 1, 2, 3, [Object
Object]
// The result is exactly the same as that of foo.

Trace (FOO = bar );
// Output: false
/* The result is false.
Correct
For the reference type of array, "=" determines whether the variables are the same only when the object is referenced, rather than the same content. Because bar is the Slice Method (or
Concat method) returns a new array, so it is not referenced in the original array of the foo variable.
*/

Trace (FOO [0] =
Bar [0]);
// Output: True
// Note that the first element is of the value type, so "=" is determined based on the value.
Trace (FOO [1]
= Bar [1]);
Trace (FOO [2] =
Bar [2]):
/*
Output:
True
True
The two elements are of reference type, and "=" is determined based on whether the reference is the same
*/

// 3.
Next we will change all the elements of the bar array, and compare Foo and bar one by one.
Bar [0] = "A New String in
Bar ";
Bar [1] [0] = 1000;
Bar [2]. Web = "www.actiongscript3.cn
";

Trace (FOO );
// Output: a string
Element, 1000,2, 3, [Object
Object]
// Note that apart from the first element, the content in Foo also changes with bar. Changed bar and
Foo
// Reference the content of a type element
Trace (bar );
// Output: A New String in bar
, 1000,2, 3, [Object
Object]
// You Can See That bar is the same except the first element and the current Foo element.

// How do I know that the third element has changed? Let me trace its Web attributes.
Trace
(FOO [2]. Web );
// Output: www.actionscript3.cn


// The Web attribute of the third element in Foo has also changed
Trace (BAR [2]. Web );


3. Deep Replication

Using a new array that is generated in the deep recovery mode, all its elements are backups of real array elements. In this case, the two arrays share the same unknown element, which stores different references and points to different objects. However, the two exclusive states are completely consistent.

How to generate deep replication? This requires the use of powerfulBytearray
Class. See the example for the method:
Example: In-depth copying of Arrays: bytearray
Application


VaR objectelement: Object = {name: "kingda", Web: "www.kingda.org"
};
VaR
Foo: array = ["a string element", arrayelement, objectelement];
// Copy the array in the following four rows
Foo
VaR fooba: bytearray = new
Bytearray ();
Fooba. writeobject (FOO );
Fooba. Position = 0;
VaR bar: Array
= Fooba, readobject () as array;

Trace (bar );
// Output: a string
Element, 1, 2, 3, [object]
Trace (FOO =
Bar );
// Output: false
// It indicates that Foo and bar hold the reference of two different objects.
Trace (FOO [0] =
Bar [0]);
// Output: True
// Because the first element is of the string type, "=" indicates that the value is equal and returns true.
Trace (FOO [1] =
Bar [1]);
// Output: false
// The second element is of the array type. Therefore, "=" is used to determine whether the reference is the same. If it is different, false is returned.
Trace (FOO [2]
=
Bar [2]);
// Output: false
// The third element is of the object type. Therefore, "=" is used to determine whether the reference is the same. If it is different, false is returned.

// Change the bar element in the following three rows
Bar [0]
= "A New String in bar ";
Bar [1] [0] = 1000;
Bar [2]. Web = "www.actionscript3.cn"
;
// Check whether foo is affected.
Trace
(FOO );
// Output: A string element, 1, 2, 3, [object]
Trace (bar );
// Output:
New String in bar, 1000,2, 3, [object]
Trace (FOO [2]. Web );
// Output: www.kngda.org


Trace
(BAR [2]. Web );
// Output: www.actionscript3.cn
// It can be seen that bar changes do not affect Foo, and actually all elements are actually copied.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.