Encapsulation CAN Make Things Change-Create a Container That Demonstrates The "Resize" of an Array

xiaoxiao2021-03-06  160

IT is generally belived that overce an array has been initialized, ITS Size is unmodifiable. This concept is right.

But Through Composition, So Called "Encapsulation", We can it it posible to "change" the size of an array by changing its reason.

As we all know, Java has a quite different mechanism from C does that it has no pointer but references which are used to point at Objects and when you change the reference, the Object of it will not be modified.

When You Say:

String a = "a";

String b = "b";

String c = B;

And then:

B = a;

The Result IS:

a = "a";

B = "a";

C = "b";

Because B Points to Object That Reference "a" is pointing to. And no ojbect was change.

So, We can "change" an Array's Length by Changing ITS Reference:

String [] str1 = {"0", "1", "2"};

String [] str2 = new string [4];

System.ArrayCopy (0, STR1, 0, STR2, STR1.LENGTH);

STR1 = STR2;

And now, str1 HAS One More Element to use;

The Following Program Demostrates this:

转载请注明原文地址:https://www.9cbs.com/read-128166.html

New Post(0)