Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  ToArray Method
ToArray  
Method  
Copies elements from the ArrayList to a new single dimension array
ArrayList Class
System.Collections NameSpace
CF1.  Function ToArray ( ) : System.Array ;
CF2.  Function ToArray ( ElementType : Type; ) : System.Array;
CF : Methods with this mark are Compact Framework Compatible
Description
The ToArray method builds a new Array, and fills it with all the element values from the current ArrayList.
 
The data type of the array can be forced to ElementType, but an exception will be thrown if the ToArray method cannot cast all ArrayList element values to this type.
 
Just like CopyTo, ToArray performs a shallow copy. When the current ArrayList 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.
Notes
ArrayLists and Arrays are 0 index based.
Microsoft MSDN Links
System.Collections
System.Collections.ArrayList
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList  : System.Collections.ArrayList;
  MyArray : System.Array;
  i       : Integer;

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

  // Fill it
  MyList.Add('Data');
  MyList.Add('from');
  MyList.Add('an');
  MyList.Add('ArrayList');

  // Copy to the array
  MyArray := MyList.ToArray;

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  Console.Readline;
end.
Show full unit code
  Data
  from
  an
  ArrayList
Copying from an ArrayList of different element types
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  MyList  : System.Collections.ArrayList;
  MyArray : System.Array;
  i       : Integer;

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

  // Fill it
  MyList.Add('Name :');
  MyList.Add('Neil Moffatt');
  MyList.Add('Age :');
  MyList.Add(TObject(47));
  MyList.Add('Birthday :');
  MyList.Add(DateTime.Create(1957, 2, 18));

  // Copy to the array
  Console.WriteLine('Copying from ArrayList to the array :');
  Console.WriteLine;

  MyArray := MyList.ToArray;

  // Display the array contents
  for i := 0 to MyArray.Length-1 do
    Console.WriteLine(MyArray.GetValue(i).ToString);

  Console.Readline;
end.
Show full unit code
  Copying from ArrayList to the array :
  
  Name :
  Neil Moffatt
  Age :
  47
  Birthday :
  18/02/1957 00:00:00
Illustrating the shallowness of ToArray element copying
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
  MyList   : System.Collections.ArrayList;
  MyArray  : System.Array;
  MyObject : MyClass;
  i        : Integer;

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

  // Fill it
  MyList.Add(MyClass.Create('Hello'));
  MyList.Add(MyClass.Create('cruel'));
  MyList.Add(MyClass.Create('World'));

  // Copy to the array
  Console.WriteLine('Copying from ArrayList to the array :');
  Console.WriteLine;

  MyArray := MyList.ToArray;

  // Display both array contents
  for i := 0 to MyList.Count-1 do
  begin
    MyObject := MyClass(MyList[i]);
    Console.WriteLine('MyList[{0}]  = {1}',
                      i.ToString,
                      MyObject.ToString);
  end;

  Console.WriteLine;

  for i := 0 to MyArray.Length-1 do
  begin
    MyObject := MyClass(MyArray.GetValue(i));
    Console.WriteLine('MyArray[{0}] = {1}',
                      i.ToString,
                      MyObject.ToString);
  end;

  // Demonstrate the shallowness of the  process
  Console.WriteLine;
  MyObject := MyClass(MyList[1]);
  MyObject.Name := 'wonderful';  // Was 'sad'

  Console.WriteLine('After updating just MyArray :');
  Console.WriteLine;

  // Display both array contents again
  for i := 0 to MyList.Count-1 do
  begin
    MyObject := MyClass(MyList[i]);
    Console.WriteLine('MyList[{0}]  = {1}',
                      i.ToString,
                      MyObject.ToString);
  end;

  Console.WriteLine;

  for i := 0 to MyArray.Length-1 do
  begin
    MyObject := MyClass(MyArray.GetValue(i));
    Console.WriteLine('MyArray[{0}] = {1}',
                      i.ToString,
                      MyObject.ToString);
  end;

  Console.Readline;
end.
Show full unit code
  Copying from ArrayList to the array :
  
  MyList[0]  = Hello
  MyList[1]  = cruel
  MyList[2]  = World
  
  MyArray[0] = Hello
  MyArray[1] = cruel
  MyArray[2] = World
  
  After updating just MyArray :
  
  MyList[0]  = Hello
  MyList[1]  = wonderful
  MyList[2]  = World
  
  MyArray[0] = Hello
  MyArray[1] = wonderful
  MyArray[2] = World
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author