Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  GetRange Method
  
GetRange  
Method  
Creates a new ArrayList from a subset of the current ArrayList elements
ArrayList Class
System.Collections NameSpace
NotCF1.  Procedure GetRange ( StartIndex : Integer; Count : Integer ) ;
CF : Methods with this mark are Compact Framework Compatible
Description
The GetRange method is awkwardly named. It is really a variation of the Clone method - a new ArrayList is built, and set from element values from the current ArrayList.
 
Count elements from StartIndex of the current ArrayList are copied to the new ArrayList.
 
Just like Clone, GetRange performs a shallow copy. When the current array holds reference (non primitive) data types, the target array element values still refer to the same objects that current array elements refer to. This is what is referred to as a shallow copy. A deep copy would create new versions of the referred objects.
Microsoft MSDN Links
System.Collections
System.Collections.ArrayList
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  Source, Target : System.Collections.ArrayList;
  i              : Integer;

begin
  // Create our array list object
  Source := ArrayList.Create;

  // Fill it
  Source.Add('0.Zero');
  Source.Add('1.One');
  Source.Add('2.Two');
  Source.Add('3.Three');
  Source.Add('4.Four');
  Source.Add('5.Five');
  Source.Add('6.Six');
  Source.Add('7.Seven');
  Source.Add('8.Eight');
  Source.Add('9.Nine');

  // Create a new ArrayList with just the middle 4 elements
  Console.WriteLine('Copying the middle 4 elements ');
  Console.WriteLine('to a new ArrayList :');
  Console.WriteLine;
  Target := Source.GetRange(3, 4);

  // Display the new array contents
  for i := 0 to Target.Count-1 do
    Console.WriteLine(Target[i].ToString);

  Console.Readline;
end.
   Copying the middle 4 elements
   to a new ArrayList :
  
   3.Three
   4.Four
   5.Five
   6.Six
Illustrating the shallowness of the GetRange element copy
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

type
  MyClass = Class
    private
      mcName : String;
    published
      Property Name : String
          read mcName
         write mcName;
      Constructor Create(Name : String); overload;
      Function    ToString : String;     overload;
  end;

// MyClass methods
constructor MyClass.Create(Name : String);
begin
  inherited Create;
  mcName := Name;
end;

function MyClass.ToString : String;
begin
  Result := Name;
end;

// Main code

var
  myArray, cloneArray : System.Collections.ArrayList;
  myObject            : MyClass;
  i                   : Integer;

begin
  // Create a 3 element array of strings
  myArray := ArrayList.Create;
  myArray.Add(MyClass.Create('Hello'));
  myArray.Add(MyClass.Create('sad'));
  myArray.Add(MyClass.Create('World'));

  // Clone the array - this carries out a shallow copy
  cloneArray := myArray.GetRange(0, 3);

  // Display both arrays
  for i := 0 to myArray.Count-1 do
  begin
    myObject := MyClass(myArray.Item[i]);
    Console.WriteLine('myArray[{0}]    = {1}',
                       i.ToString, myObject.ToString);
  end;

  Console.WriteLine;

  for i := 0 to cloneArray.Count-1 do
  begin
    myObject := MyClass(cloneArray.Item[i]);
    Console.WriteLine('cloneArray[{0}] = {1}',
                       i.ToString, myObject.ToString);
  end;

  // Demonstrate the shallowness of the GetRange process
  Console.WriteLine;
  myObject := MyClass(cloneArray.Item[1]);
  myObject.Name := 'wonderful';  // Was 'sad'

  Console.WriteLine('After updating just the cloneArray :');
  Console.WriteLine;

  // Display both arrays again
  for i := 0 to myArray.Count-1 do
  begin
    myObject := MyClass(myArray.Item[i]);
    Console.WriteLine('myArray[{0}]    = {1}',
                       i.ToString, myObject.ToString);
  end;

  Console.WriteLine;

  for i := 0 to cloneArray.Count-1 do
  begin
    myObject := MyClass(cloneArray.Item[i]);
    Console.WriteLine('cloneArray[{0}] = {1}',
                       i.ToString, myObject.ToString);
  end;

  Console.ReadLine;
end.
   myArray[0]    = Hello
   myArray[1]    = sad
   myArray[2]    = World
  
   cloneArray[0] = Hello
   cloneArray[1] = sad
   cloneArray[2] = World
  
   After updating just the cloneArray :
  
   myArray[0]    = Hello
   myArray[1]    = wonderful
   myArray[2]    = World
  
   cloneArray[0] = Hello
   cloneArray[1] = wonderful
   cloneArray[2] = World
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author