Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  LastIndexOf Method
LastIndexOf  
Method  
Tries to find the alst occurence of an object in the current ArrayList
ArrayList Class
System.Collections NameSpace
CF1.  Function LastIndexOf ( Value : Object; ) : Integer;
CF2.  Function LastIndexOf ( Value:ObjectValue : Object; BackFromIndex : Integer; ) : Integer;
NotCF3.  Function LastIndexOf ( Value:ObjectValue : Object; BackFromIndex : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The current ArrayList is searched for the last occurence of the given Value object. If found, the element index of the object is returned, otherwise -1 is returned.
 
The array is scanned from the end to start unles the optional BackFromIndex and Count parameters are provided to give a start and range of elements as appropriate.
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
  List  : System.Collections.ArrayList;
  index : Integer;

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

  // Fill it
  List.Add('ABC');
  List.Add('DEF');
  List.Add('ABC');
  List.Add('GHI');

  // Try to find 'ABC'
  index := List.LastIndexOf('ABC');

  Console.WriteLine('Last position of ''ABC'' is {0}', index.ToString);

  // Try to find 'abc'
  index := List.LastIndexOf('abc');

  Console.WriteLine('Last position of ''abc'' is {0}', index.ToString);

  Console.Readline;
end.
Show full unit code
  Last position of 'ABC' is 2
  Last position of 'abc' is -1
Using the optional FromIndex and Count parameters
program Project1;
{$APPTYPE CONSOLE}

uses
  System.Collections;

var
  List  : System.Collections.ArrayList;
  index : Integer;

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

  // Fill it
  List.Add('ABC');
  List.Add('DEF');
  List.Add('ABC');
  List.Add('GHI');
  List.Add('DEF');
  List.Add('GHI');

  // Try to find the position of 'ABC' in elements 2 down to 0
  index := List.LastIndexOf('ABC', 2, 3);

  Console.WriteLine('''ABC'' from 2 down to 0 = {0}',
                    index.ToString);

  // Try to find 'ABC' in elements 5 down to to 3
  index := List.LastIndexOf('ABC', 5, 3);

  Console.WriteLine('''ABC'' from 5 down to 3 = {0}',
                    index.ToString);

  Console.Readline;
end.
Show full unit code
  'ABC' from 2 down to 0 = 2
  'ABC' from 5 down to 3 = -1
  
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author