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
|
|
|
|