Array.IndexOf 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
搜尋指定的物件,並傳回其在一維陣列或陣列中某個項目範圍內第一次出現的索引。
多載
IndexOf(Array, Object) |
搜尋指定的物件,並傳回一維陣列中第一個相符項目的索引。 |
IndexOf(Array, Object, Int32) |
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍從指定的索引延伸到陣列的結尾。 |
IndexOf(Array, Object, Int32, Int32) |
在一維陣列的項目範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍的延伸起點為指定項目數的指定索引。 |
IndexOf<T>(T[], T, Int32) |
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍從指定的索引延伸到陣列的結尾。 |
IndexOf<T>(T[], T, Int32, Int32) |
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍的延伸起點為指定項目數的指定索引。 |
IndexOf<T>(T[], T) |
搜尋指定的物件,並傳回一維陣列中第一個相符項目的索引。 |
IndexOf(Array, Object)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
搜尋指定的物件,並傳回一維陣列中第一個相符項目的索引。
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
參數
- array
- Array
要搜尋的一維陣列。
- value
- Object
要在 array
中尋找的物件。
傳回
如有找到,則為 array
中 value
第一個相符項目的索引;否則為陣列的下限減 1。
例外狀況
array
為 null
。
array
是多維的。
範例
此範例會呼叫 方法的 IndexOf 下列三個多載,以在字串陣列中尋找字串的索引:
IndexOf(Array, Object),以判斷字串陣列中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32),以判斷字串陣列第四個元素中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32, Int32),從最後一次成功比對到陣列結尾的專案,判斷字串陣列中第一個出現的字串 「the」。
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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 <= strings->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 <= strings.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.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = 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.
Dim position As Integer = 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)
End Sub
End Module
' 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.
備註
這個方法會搜尋一維陣列 value
的所有專案。 若要判斷 value
是否存在於 中 array
,方法會呼叫每個元素的 Equals
方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals(Object) 方法,則會呼叫該覆寫。
因為大部分陣列的下限為零,所以如果 value
找不到,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value
找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。
這個方法是 O (n
) 作業,其中 n
是 Length 的 array
。
另請參閱
適用於
IndexOf(Array, Object, Int32)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍從指定的索引延伸到陣列的結尾。
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex);
public static int IndexOf (Array array, object value, int startIndex);
public static int IndexOf (Array array, object? value, int startIndex);
static member IndexOf : Array * obj * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer) As Integer
參數
- array
- Array
要搜尋的一維陣列。
- value
- Object
要在 array
中尋找的物件。
- startIndex
- Int32
搜尋的起始索引。 0 (零) 在空白陣列中有效。
傳回
如有找到,則為 array
中從 startIndex
延伸至最後一個項目的項目範圍內,第一次出現之 value
的索引;否則為陣列的下限減 1。
例外狀況
array
為 null
。
startIndex
超出 array
的有效索引範圍。
array
是多維的。
範例
此範例會呼叫 方法的 IndexOf 下列三個多載,以在字串陣列中尋找字串的索引:
IndexOf(Array, Object),以判斷字串陣列中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32),以判斷字串陣列第四個元素中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32, Int32),從最後一次成功比對到陣列結尾的專案,判斷字串陣列中第一個出現的字串 「the」。
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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 <= strings->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 <= strings.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.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = 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.
Dim position As Integer = 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)
End Sub
End Module
' 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.
備註
這個方法會從索引 startIndex
處的 元素到最後一個專案搜尋一維陣列。 若要判斷 value
是否存在於 中 array
,方法會呼叫 Equals
每個元素的 方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals(Object) 方法,則會呼叫該覆寫。
因為大部分陣列的下限為零,所以如果 value
找不到,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value
找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。
如果 startIndex
等於 Array.Length ,方法會傳回 -1。 如果 startIndex
大於 Array.Length ,方法會 ArgumentOutOfRangeException 擲回 。
這個方法是 O (n
) 作業,其中 n
是從 到 結尾的專案 array
數目 startIndex
。
另請參閱
適用於
IndexOf(Array, Object, Int32, Int32)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
在一維陣列的項目範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍的延伸起點為指定項目數的指定索引。
public:
static int IndexOf(Array ^ array, System::Object ^ value, int startIndex, int count);
public static int IndexOf (Array array, object value, int startIndex, int count);
public static int IndexOf (Array array, object? value, int startIndex, int count);
static member IndexOf : Array * obj * int * int -> int
Public Shared Function IndexOf (array As Array, value As Object, startIndex As Integer, count As Integer) As Integer
參數
- array
- Array
要搜尋的一維陣列。
- value
- Object
要在 array
中尋找的物件。
- startIndex
- Int32
搜尋的起始索引。 0 (零) 在空白陣列中有效。
- count
- Int32
要搜尋的項目數目。
傳回
如有找到,則為 array
中從索引 startIndex
到 startIndex
+ count
-1 的範圍內,第一次出現之 value
的索引;否則為陣列的下限減 1。
例外狀況
array
為 null
。
startIndex
超出 array
的有效索引範圍。
-或-
count
小於零。
-或-
startIndex
和 count
未指定 array
中的有效區段。
array
是多維的。
範例
此範例會呼叫 方法的 IndexOf 下列三個多載,以在字串陣列中尋找字串的索引:
IndexOf(Array, Object),以判斷字串陣列中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32),以判斷字串陣列第四個元素中第一個出現的字串 「the」。
IndexOf(Array, Object, Int32, Int32),從最後一次成功比對到陣列結尾的專案,判斷字串陣列中第一個出現的字串 「the」。 若要判斷引數的值
count
,它會從起始索引減去陣列的上限,並加入一個。
using namespace System;
void main()
{
// Create a string array with 3 elements having the same value.
array<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 <= strings->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 <= strings.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.
Public Module Example
Public Sub Main()
' Create a string array with 3 elements having the same value.
Dim strings() As String = { "the", "quick", "brown", "fox",
"jumps", "over", "the", "lazy",
"dog", "in", "the", "barn" }
' Display the values of the array.
Console.WriteLine("The array contains the following values:")
For i As Integer = strings.GetLowerBound(0) To strings.GetUpperBound(0)
Console.WriteLine(" [{0,2}]: {1}", i, strings(i))
Next
' Search for the first occurrence of the duplicated value.
Dim searchString As String = "the"
Dim index As Integer = 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.
Dim position As Integer = 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)
End Sub
End Module
' 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.
備註
如果 count
大於 0,這個方法會將一維陣列的專案從 startIndex
搜尋為 startIndex
加 count
減 1。 若要判斷 value
是否存在於 中 array
,方法會呼叫 Equals
每個元素的 方法,直到找到相符專案為止,執行相等比較。 這表示,如果專案覆寫 Object.Equals 方法,則會呼叫該覆寫。
因為大部分陣列的下限為零,所以找不到時 value
,這個方法通常會傳回 -1。 在罕見的情況下,陣列的下限等於 Int32.MinValue (0x80000000) 且 value
找不到,此方法會 Int32.MaxValue 傳回 (0x7FFFFFFF) 。
如果 startindex
等於 Array.Length ,則方法會傳回 -1。 如果 startIndex
大於 Array.Length ,方法會 ArgumentOutOfRangeException 擲回 。
這個方法是 O (n
) 作業,其中 n
是 count
。
另請參閱
適用於
IndexOf<T>(T[], T, Int32)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍從指定的索引延伸到陣列的結尾。
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex);
public static int IndexOf<T> (T[] array, T value, int startIndex);
static member IndexOf : 'T[] * 'T * int -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T, startIndex As Integer) As Integer
類型參數
- T
陣列項目的類型。
參數
- array
- T[]
要搜尋的一維、以零為起始的陣列。
- value
- T
要在 array
中尋找的物件。
- startIndex
- Int32
搜尋之以零為起始的起始索引。 0 (零) 在空白陣列中有效。
傳回
array
中從 startIndex
到最後一個項目的項目範圍內,第一次出現 value
之以零為起始的索引;如未找到則為 -1。
例外狀況
array
為 null
。
startIndex
超出 array
的有效索引範圍。
範例
下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續到陣列結尾,並尋找第二個出現的字串。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。
using namespace System;
void main()
{
array<String^>^ 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
備註
這個方法會從 位於 startIndex
的 元素搜尋到陣列結尾的一維陣列。 若要判斷 value
是否存在於 array
中,方法會在每個元素上呼叫 T.Equals
方法來執行相等比較。 這表示如果 T
覆寫 Equals 方法,則會呼叫該覆寫。
如果 startIndex
等於 Length ,則方法會傳回 -1。 如果 startIndex
大於 Array.Length ,則方法會擲回 ArgumentOutOfRangeException 。
這個方法是 O (n
) 作業,其中 n
是 從 startIndex
到 結尾的專案 array
數目。
另請參閱
適用於
IndexOf<T>(T[], T, Int32, Int32)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
在一維陣列的元素範圍中搜尋指定的物件,並傳回其第一次出現的索引。 此範圍的延伸起點為指定項目數的指定索引。
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value, int startIndex, int count);
public static int IndexOf<T> (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
類型參數
- T
陣列項目的類型。
參數
- array
- T[]
要搜尋的一維、以零為起始的陣列。
- value
- T
要在 array
中尋找的物件。
- startIndex
- Int32
搜尋之以零為起始的起始索引。 0 (零) 在空白陣列中有效。
- count
- Int32
區段中要搜尋的項目數目。
傳回
在 array
中從 startIndex
開始且包含 count
中指定項目數的項目範圍內,第一次出現 value
之以零為起始的索引;如未找到則為 -1。
例外狀況
array
為 null
。
startIndex
超出 array
的有效索引範圍。
-或-
count
小於零。
-或-
startIndex
和 count
未指定 array
中的有效區段。
範例
下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續進行陣列結尾,並尋找字串的第二個出現專案。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。
using namespace System;
void main()
{
array<String^>^ 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
備註
如果 count
大於 0,這個方法會從 搜尋一維陣列 startIndex
的專案,以 startIndex
加 count
減 1。 若要判斷 value
是否存在於 array
中,方法會在每個元素上呼叫 T.Equals
方法來執行相等比較。 這表示如果 T
覆寫 Equals 方法,則會呼叫該覆寫。
如果 startIndex
等於 Array.Length ,則方法會傳回 -1。 如果 startIndex
大於 Array.Length ,則方法會擲回 ArgumentOutOfRangeException 。
這個方法是 O (n
) 作業,其中 n
是 count
。
另請參閱
適用於
IndexOf<T>(T[], T)
- 來源:
- Array.cs
- 來源:
- Array.cs
- 來源:
- Array.cs
搜尋指定的物件,並傳回一維陣列中第一個相符項目的索引。
public:
generic <typename T>
static int IndexOf(cli::array <T> ^ array, T value);
public static int IndexOf<T> (T[] array, T value);
static member IndexOf : 'T[] * 'T -> int
Public Shared Function IndexOf(Of T) (array As T(), value As T) As Integer
類型參數
- T
陣列項目的類型。
參數
- array
- T[]
要搜尋的一維、以零為起始的陣列。
- value
- T
要在 array
中尋找的物件。
傳回
整個 array
中第一次出現 value
之以零為起始的索引;如未找到則為 -1。
例外狀況
array
為 null
。
範例
下列範例示範 方法的所有三個泛型多載 IndexOf 。 系統會建立字串陣列,其中一個專案會出現兩次,位於索引位置 0 和索引位置 5。 方法 IndexOf<T>(T[], T) 多載會從開頭搜尋陣列,並尋找第一個出現的字串。 方法 IndexOf<T>(T[], T, Int32) 多載是用來搜尋開頭為索引位置 3 的陣列,並繼續進行陣列結尾,並尋找字串的第二個出現專案。 最後, IndexOf<T>(T[], T, Int32, Int32) 方法多載是用來搜尋兩個專案的範圍,從索引位置 2 開始;它會傳回 -1,因為該範圍中沒有搜尋字串的實例。
using namespace System;
void main()
{
array<String^>^ 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
備註
這個方法會搜尋一維陣列的所有專案。 value
若要判斷 value
是否存在於 array
中,方法會在每個元素上呼叫 T.Equals
方法來執行相等比較。 這表示如果 T
覆寫 Equals 方法,則會呼叫該覆寫。
這個方法是 O (n
) 作業,其中 n
是 Length 的 array
。