Get index of string list java

Array.IndexOf Method

  • Reference
Is this page helpful?

Please rate your experience

Yes No
Any additional feedback?

Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services. Privacy policy.

Submit

Thank you.

Definition

Namespace: System Assembly:System.Runtime.dll Assembly:mscorlib.dll Assembly:netstandard.dll

Important

Some information relates to prerelease product that may be substantially modified before its released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array or in a range of elements in the array.

In this article

Overloads

IndexOf[Array, Object]

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

IndexOf[Array, Object, Int32]

Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. The range extends from a specified index to the end of the array.

IndexOf[Array, Object, Int32, Int32]

Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of ifs first occurrence. The range extends from a specified index for a specified number of elements.

IndexOf[T[], T, Int32]

Searches for the specified object in a range of elements of a one dimensional array, and returns the index of its first occurrence. The range extends from a specified index to the end of the array.

IndexOf[T[], T, Int32, Int32]

Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. The range extends from a specified index for a specified number of elements.

IndexOf[T[], T]

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

IndexOf[Array, Object]

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

public: static int IndexOf[Array ^ array, System::Object ^ value]; public static int IndexOf [Array array, object value]; public static int IndexOf [Array array, object? value]; static member IndexOf : Array * obj -> int Public Shared Function IndexOf [array As Array, value As Object] As Integer

Parameters

array Array

The one-dimensional array to search.

value Object

The object to locate in array.

Returns

Int32

The index of the first occurrence of value in array, if found; otherwise, the lower bound of the array minus 1.

Exceptions

ArgumentNullException

array is null.

RankException

array is multidimensional.

Examples

The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:

  • IndexOf[Array, Object], to determine the first occurrence of the string "the" in a string array.

  • IndexOf[Array, Object, Int32], to determine the first occurrence of the string "the" in the fourth to the last elements of a string array.

  • IndexOf[Array, Object, Int32, Int32], to determine the first occurrence of the string "the" in a string array from the element that follows the last successful match to the end of the array.

using namespace System; void main[] { // Create a string array with 3 elements having the same value. array^ strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console::WriteLine["The array contains the following values:"]; for [int i = strings->GetLowerBound[0]; i GetUpperBound[0]; i++] Console::WriteLine[" [{0,2}]: {1}", i, strings[i]]; // Search for the first occurrence of the duplicated value. String^ searchString = "the"; int index = Array::IndexOf[strings, searchString]; Console::WriteLine["The first occurrence of \"{0}\" is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in the last section of the array. index = Array::IndexOf[ strings, searchString, 4]; Console::WriteLine["The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in a section of the array. int position = index + 1; index = Array::IndexOf[strings, searchString, position, strings->GetUpperBound[0] - position + 1]; Console::WriteLine["The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.", searchString, position, strings->GetUpperBound[0], index]; } // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. let strings = [| "the"; "quick"; "brown"; "fox"; "jumps"; "over" "the"; "lazy"; "dog"; "in"; "the"; "barn" |] // Display the elements of the array. printfn "The array contains the following values:" for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do printfn $" [{i,2}]: {strings[i]}" // Search for the first occurrence of the duplicated value. let searchString = "the" let index = Array.IndexOf[strings, searchString] printfn $"The first occurrence of \"{searchString}\" is at index {index}." // Search for the first occurrence of the duplicated value in the last section of the array. let index = Array.IndexOf[strings, searchString, 4] printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}." // Search for the first occurrence of the duplicated value in a section of the array. let position = index + 1 let index = Array.IndexOf[strings, searchString, position, strings.GetUpperBound 0 - position + 1] printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}." // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. String[] strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console.WriteLine["The array contains the following values:"]; for [int i = strings.GetLowerBound[0]; i GetLowerBound[0]; i GetUpperBound[0]; i++] Console::WriteLine[" [{0,2}]: {1}", i, strings[i]]; // Search for the first occurrence of the duplicated value. String^ searchString = "the"; int index = Array::IndexOf[strings, searchString]; Console::WriteLine["The first occurrence of \"{0}\" is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in the last section of the array. index = Array::IndexOf[ strings, searchString, 4]; Console::WriteLine["The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in a section of the array. int position = index + 1; index = Array::IndexOf[strings, searchString, position, strings->GetUpperBound[0] - position + 1]; Console::WriteLine["The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.", searchString, position, strings->GetUpperBound[0], index]; } // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. let strings = [| "the"; "quick"; "brown"; "fox"; "jumps"; "over" "the"; "lazy"; "dog"; "in"; "the"; "barn" |] // Display the elements of the array. printfn "The array contains the following values:" for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do printfn $" [{i,2}]: {strings[i]}" // Search for the first occurrence of the duplicated value. let searchString = "the" let index = Array.IndexOf[strings, searchString] printfn $"The first occurrence of \"{searchString}\" is at index {index}." // Search for the first occurrence of the duplicated value in the last section of the array. let index = Array.IndexOf[strings, searchString, 4] printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}." // Search for the first occurrence of the duplicated value in a section of the array. let position = index + 1 let index = Array.IndexOf[strings, searchString, position, strings.GetUpperBound 0 - position + 1] printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}." // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. String[] strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console.WriteLine["The array contains the following values:"]; for [int i = strings.GetLowerBound[0]; i int Public Shared Function IndexOf [array As Array, value As Object, startIndex As Integer, count As Integer] As Integer

Parameters

array Array

The one-dimensional array to search.

value Object

The object to locate in array.

startIndex Int32

The starting index of the search. 0 [zero] is valid in an empty array.

count Int32

The number of elements to search.

Returns

Int32

The index of the first occurrence of value, if it's found in the array from index startIndex to startIndex + count - 1; otherwise, the lower bound of the array minus 1.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in array.

RankException

array is multidimensional.

Examples

The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:

  • IndexOf[Array, Object], to determine the first occurrence of the string "the" in a string array.

  • IndexOf[Array, Object, Int32], to determine the first occurrence of the string "the" in the fourth to the last elements of a string array.

  • IndexOf[Array, Object, Int32, Int32], to determine the first occurrence of the string "the" in a string array from the element that follows the last successful match to the end of the array. To determine the value of the count argument, it subtracts the upper bound of the array from the starting index and adds one.

using namespace System; void main[] { // Create a string array with 3 elements having the same value. array^ strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console::WriteLine["The array contains the following values:"]; for [int i = strings->GetLowerBound[0]; i GetUpperBound[0]; i++] Console::WriteLine[" [{0,2}]: {1}", i, strings[i]]; // Search for the first occurrence of the duplicated value. String^ searchString = "the"; int index = Array::IndexOf[strings, searchString]; Console::WriteLine["The first occurrence of \"{0}\" is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in the last section of the array. index = Array::IndexOf[ strings, searchString, 4]; Console::WriteLine["The first occurrence of \"{0}\" between index 4 and the end is at index {1}.", searchString, index]; // Search for the first occurrence of the duplicated value in a section of the array. int position = index + 1; index = Array::IndexOf[strings, searchString, position, strings->GetUpperBound[0] - position + 1]; Console::WriteLine["The first occurrence of \"{0}\" between index {1} and index {2} is at index {3}.", searchString, position, strings->GetUpperBound[0], index]; } // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. let strings = [| "the"; "quick"; "brown"; "fox"; "jumps"; "over" "the"; "lazy"; "dog"; "in"; "the"; "barn" |] // Display the elements of the array. printfn "The array contains the following values:" for i = strings.GetLowerBound 0 to strings.GetUpperBound 0 do printfn $" [{i,2}]: {strings[i]}" // Search for the first occurrence of the duplicated value. let searchString = "the" let index = Array.IndexOf[strings, searchString] printfn $"The first occurrence of \"{searchString}\" is at index {index}." // Search for the first occurrence of the duplicated value in the last section of the array. let index = Array.IndexOf[strings, searchString, 4] printfn $"The first occurrence of \"{searchString}\" between index 4 and the end is at index {index}." // Search for the first occurrence of the duplicated value in a section of the array. let position = index + 1 let index = Array.IndexOf[strings, searchString, position, strings.GetUpperBound 0 - position + 1] printfn $"The first occurrence of \"{searchString}\" between index {position} and index {strings.GetUpperBound 0} is at index {index}." // The example displays the following output: // The array contains the following values: // [ 0]: the // [ 1]: quick // [ 2]: brown // [ 3]: fox // [ 4]: jumps // [ 5]: over // [ 6]: the // [ 7]: lazy // [ 8]: dog // [ 9]: in // [10]: the // [11]: barn // The first occurrence of "the" is at index 0. // The first occurrence of "the" between index 4 and the end is at index 6. // The first occurrence of "the" between index 7 and index 11 is at index 10. // Create a string array with 3 elements having the same value. String[] strings = { "the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog", "in", "the", "barn" }; // Display the elements of the array. Console.WriteLine["The array contains the following values:"]; for [int i = strings.GetLowerBound[0]; i int Public Shared Function IndexOf[Of T] [array As T[], value As T, startIndex As Integer] As Integer

Type Parameters

T

The type of the elements of the array.

Parameters

array T[]

The one-dimensional, zero-based array to search.

value T

The object to locate in array.

startIndex Int32

The zero-based starting index of the search. 0 [zero] is valid in an empty array.

Returns

Int32

The zero-based index of the first occurrence of value within the range of elements in array that extends from startIndex to the last element, if found; otherwise, -1.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

Examples

The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1

Remarks

This method searches a one-dimensional array from the element at startIndex to the end of the array. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.

If startIndex equals Length,the method returns -1. If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

This method is an O[n] operation, where n is the number of elements from startIndex to the end of array.

See also

  • LastIndexOf
  • Performing Culture-Insensitive String Operations in Arrays

Applies to

IndexOf[T[], T, Int32, Int32]

Searches for the specified object in a range of elements of a one-dimensional array, and returns the index of its first occurrence. The range extends from a specified index for a specified number of elements.

public: generic static int IndexOf[cli::array ^ array, T value, int startIndex, int count]; public static int IndexOf [T[] array, T value, int startIndex, int count]; static member IndexOf : 'T[] * 'T * int * int -> int Public Shared Function IndexOf[Of T] [array As T[], value As T, startIndex As Integer, count As Integer] As Integer

Type Parameters

T

The type of the elements of the array.

Parameters

array T[]

The one-dimensional, zero-based array to search.

value T

The object to locate in array.

startIndex Int32

The zero-based starting index of the search. 0 [zero] is valid in an empty array.

count Int32

The number of elements in the section to search.

Returns

Int32

The zero-based index of the first occurrence of value within the range of elements in array that starts at startIndex and contains the number of elements specified in count, if found; otherwise, -1.

Exceptions

ArgumentNullException

array is null.

ArgumentOutOfRangeException

startIndex is outside the range of valid indexes for array.

-or-

count is less than zero.

-or-

startIndex and count do not specify a valid section in array.

Examples

The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1

Remarks

This method searches the elements of a one-dimensional array from startIndex to startIndex plus count minus 1, if count is greater than 0. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.

If startIndex equals Array.Length, the method returns -1. If startIndex is greater than Array.Length, the method throws an ArgumentOutOfRangeException.

This method is an O[n] operation, where n is count.

See also

  • LastIndexOf
  • Performing Culture-Insensitive String Operations in Arrays

Applies to

IndexOf[T[], T]

Searches for the specified object and returns the index of its first occurrence in a one-dimensional array.

public: generic static int IndexOf[cli::array ^ array, T value]; public static int IndexOf [T[] array, T value]; static member IndexOf : 'T[] * 'T -> int Public Shared Function IndexOf[Of T] [array As T[], value As T] As Integer

Type Parameters

T

The type of the elements of the array.

Parameters

array T[]

The one-dimensional, zero-based array to search.

value T

The object to locate in array.

Returns

Int32

The zero-based index of the first occurrence of value in the entire array, if found; otherwise, -1.

Exceptions

ArgumentNullException

array is null.

Examples

The following example demonstrates all three generic overloads of the IndexOf method. An array of strings is created, with one entry that appears twice, at index location 0 and index location 5. The IndexOf[T[], T] method overload searches the array from the beginning, and finds the first occurrence of the string. The IndexOf[T[], T, Int32] method overload is used to search the array beginning with index location 3 and continuing to the end of the array, and finds the second occurrence of the string. Finally, the IndexOf[T[], T, Int32, Int32] method overload is used to search a range of two entries, beginning at index location two; it returns -1 because there are no instances of the search string in that range.

using namespace System; void main[] { array^ dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console::WriteLine[]; for each[String^ dinosaur in dinosaurs ] { Console::WriteLine[dinosaur]; } Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus"]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console::WriteLine["\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array::IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; } /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ string[] dinosaurs = { "Tyrannosaurus", "Amargasaurus", "Mamenchisaurus", "Brachiosaurus", "Deinonychus", "Tyrannosaurus", "Compsognathus" }; Console.WriteLine[]; foreach[string dinosaur in dinosaurs] { Console.WriteLine[dinosaur]; } Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus"]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]]; Console.WriteLine[ "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: {0}", Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]]; /* This code example produces the following output: Tyrannosaurus Amargasaurus Mamenchisaurus Brachiosaurus Deinonychus Tyrannosaurus Compsognathus Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 */ open System let dinosaurs = [| "Tyrannosaurus" "Amargasaurus" "Mamenchisaurus" "Brachiosaurus" "Deinonychus" "Tyrannosaurus" "Compsognathus" |] printfn "" for dino in dinosaurs do printfn $"{dino}" Array.IndexOf[dinosaurs, "Tyrannosaurus"] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\"]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 3] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 3]: %i" Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2] |> printfn "\nArray.IndexOf[dinosaurs, \"Tyrannosaurus\", 2, 2]: %i" // This code example produces the following output: // // Tyrannosaurus // Amargasaurus // Mamenchisaurus // Brachiosaurus // Deinonychus // Tyrannosaurus // Compsognathus // // Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 // // Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1 Public Class Example Public Shared Sub Main[] Dim dinosaurs[] As String = { "Tyrannosaurus", _ "Amargasaurus", _ "Mamenchisaurus", _ "Brachiosaurus", _ "Deinonychus", _ "Tyrannosaurus", _ "Compsognathus" } Console.WriteLine[] For Each dinosaur As String In dinosaurs Console.WriteLine[dinosaur] Next Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus""]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus"]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 3]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]] Console.WriteLine[vbLf & _ "Array.IndexOf[dinosaurs, ""Tyrannosaurus"", 2, 2]: {0}", _ Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]] End Sub End Class ' This code example produces the following output: ' 'Tyrannosaurus 'Amargasaurus 'Mamenchisaurus 'Brachiosaurus 'Deinonychus 'Tyrannosaurus 'Compsognathus ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus"]: 0 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 3]: 5 ' 'Array.IndexOf[dinosaurs, "Tyrannosaurus", 2, 2]: -1

Remarks

This method searches all the elements of a one-dimensional array for value. To determine whether value exists in array, the method performs an equality comparison by calling the T.Equals method on every element. This means that if T overrides the Equals method, that override is called.

This method is an O[n] operation, where n is the Length of array.

See also

  • LastIndexOf
  • Performing Culture-Insensitive String Operations in Arrays

Applies to

Video liên quan

Chủ Đề