Home  |  Delphi .net Home  |  System.Collections.ArrayList  |  IndexOf Method
IndexOf  
Method  
Tries to find the first occurence of an object in the current ArrayList
ArrayList Class
System.Collections NameSpace
CF1.  Function IndexOf ( Value : Object; ) : Integer ;
CF2.  Function IndexOf ( Value:ObjectValue : Object; FromIndex : Integer; ) : Integer;
CF3.  Function IndexOf ( Value:ObjectValue : Object; FromIndex : Integer; Count : Integer; ) : Integer;
CF : Methods with this mark are Compact Framework Compatible
Description
The current ArrayList is searched for the first 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 start to the end unles the optional FromIndex 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.IndexOf('ABC');

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

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

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

  Console.Readline;
end.
Show full unit code
  First position of 'ABC' = 0
  First position of 'abc' = -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 'GHI' in elements 0 to 2
  index := List.IndexOf('GHI', 0, 3);

  Console.WriteLine('The index of ''GHI'' from 0 to 2 = {0}',
                    index.ToString);

  // Try to find 'GHI' in elements 3 to 5
  index := List.IndexOf('GHI', 3, 3);

  Console.WriteLine('The index of ''GHI'' from 3 to 5 = {0}',
                    index.ToString);

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