Get index of string list java
Array.IndexOf Method
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. Show
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 articleOverloads
IndexOf(Array, Object)Searches for the specified object and returns the index of its first occurrence in a one-dimensional array. Parameters
array
Array
The one-dimensional array to search.
value
Object
The object to locate in array. ReturnsInt32The 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. ExamplesThe example calls the following three overloads of the IndexOf method to find the index of a string in a string array:
RemarksThis 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 each element's Equals method until it finds a match. This means that if the element overrides the Object.Equals(Object) method, that override is called. Because most arrays have a lower bound of zero, this method generally returns -1 ifvalue isn't found. In the rare case that the lower bound of the array is equal to Int32.MinValue(0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF). This method is an O(n) operation, where n is the Length of array. See also
Applies toIndexOf(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. 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. ReturnsInt32The index of the first occurrence of value, if it's found, within the range of elements in array that extends from startIndex to the last element; 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.
RankException
array is multidimensional. ExamplesThe example calls the following three overloads of the IndexOf method to find the index of a string in a string array:
RemarksThis method searches a one-dimensional array from the element at index startIndex to the last element. To determine whether value exists in array, the method performs an equality comparison by calling the Equals method of every element until it finds a match. This means that if the element overrides the Object.Equals(Object) method, that override is called. Because most arrays have a lower bound of zero, this method generally returns -1 if value isn't found. In the rare case that the lower bound of the array is equal to Int32.MinValue(0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF). 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 the number of elements from startIndex to the end of array. See also
Applies toIndexOf(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. 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. ReturnsInt32The 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. ExamplesThe example calls the following three overloads of the IndexOf method to find the index of a string in a string array:
RemarksThis 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 Equals method of every element until it finds a match. This means that if the element overrides the Object.Equals method, that override is called. Because most arrays have a lower bound of zero, this method generally returns -1 when value isn't found. In the rare case that the lower bound of the array is equal to Int32.MinValue (0x80000000) and value isn't found, this method returns Int32.MaxValue (0x7FFFFFFF). 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
Applies to
IndexOf
|
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.
Type Parameters
The type of the elements of the array.
Parameters
The one-dimensional, zero-based array to search.
The object to locate in array.
The zero-based starting index of the search. 0 (zero) is valid in an empty array.
The number of elements in the section to search.
Returns
Int32The 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
array is null.
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
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.
Type Parameters
The type of the elements of the array.
Parameters
The one-dimensional, zero-based array to search.
The object to locate in array.
Returns
Int32The zero-based index of the first occurrence of value in the entire array, if found; otherwise, -1.
Exceptions
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
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