Array.IndexOf 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정한 개체를 검색하여 1차원 배열 또는 배열의 요소 범위에서 처음으로 일치하는 인덱스를 반환합니다.
오버로드
| IndexOf(Array, Object) |
지정한 개체를 검색하여 1차원 배열에서 처음 검색된 개체의 인덱스를 반환합니다. |
| IndexOf(Array, Object, Int32) |
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 인덱스에서 배열의 끝까지 확장됩니다. |
| IndexOf(Array, Object, Int32, Int32) |
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 요소 수에 대해 지정한 인덱스에서 확장됩니다. |
| IndexOf<T>(T[], T, Int32) |
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 인덱스에서 배열의 끝까지 확장됩니다. |
| IndexOf<T>(T[], T, Int32, Int32) |
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 요소 수에 대해 지정한 인덱스에서 확장됩니다. |
| IndexOf<T>(T[], T) |
지정한 개체를 검색하여 1차원 배열에서 처음 검색된 개체의 인덱스를 반환합니다. |
IndexOf(Array, Object)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
지정한 개체를 검색하여 1차원 배열에서 처음 검색된 개체의 인덱스를 반환합니다.
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
검색할 1차원 배열입니다.
- value
- Object
array에서 찾을 개체입니다.
반환
value가 있을 경우 array에서 처음 검색된 값의 인덱스이고, 그렇지 않으면 배열의 하한에서 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.
설명
이 메서드는 에 대한 1차원 배열의 모든 요소를 검색합니다 value. 에 array있는지 여부를 value 확인하려면 메서드는 일치 항목을 발견할 때까지 각 요소의 Equals 메서드를 호출하여 같음 비교를 수행합니다. 즉, 요소가 메서드를 재정의하는 경우 해당 재정의 Object.Equals(Object) 가 호출됩니다.
대부분의 배열은 하한이 0이므로 이 메서드는 일반적으로 찾을 수 없는 경우value -1을 반환합니다. 배열의 하한이 (0x80000000)와 같 Int32.MinValue고 value 찾을 수 없는 드문 경우 이 메서드는 (0x7FFFFFFF)를 반환 Int32.MaxValue 합니다.
이 메서드는 O(n) 작업이며 여기서 n 는 의 array입니다Length.
추가 정보
적용 대상
IndexOf(Array, Object, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 인덱스에서 배열의 끝까지 확장됩니다.
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
검색할 1차원 배열입니다.
- value
- Object
array에서 찾을 개체입니다.
- startIndex
- Int32
검색할 시작 인덱스입니다. 0은 빈 배열에서 유효합니다.
반환
value가 있을 경우 startIndex에서 마지막 요소로 확장하는 array의 요소 범위에서 처음 검색된 값의 인덱스이고, 그렇지 않으면 배열의 하한에서 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 스에서 요소에서 마지막 요소까지 1차원 배열을 검색합니다. 에 있는지 여부를 value 확인하려면 메서드는 일치 항목을 발견할 때까지 모든 요소의 메서드를 호출 Equals 하여 같음 비교를 수행array합니다. 즉, 요소가 메서드를 재정의하는 경우 해당 재정의 Object.Equals(Object) 가 호출됩니다.
대부분의 배열은 하한이 0이므로 이 메서드는 일반적으로 찾을 수 없는 경우 value -1을 반환합니다. 배열의 하한이 (0x80000000)와 같 Int32.MinValue고 value 찾을 수 없는 드문 경우 이 메서드는 (0x7FFFFFFF)를 반환 Int32.MaxValue 합니다.
가 Array.Length이면 startIndex 메서드는 -1을 반환합니다. 가 보다 Array.Length크면 startIndex 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다.
이 메서드는 O(n) 작업입니다. 여기서 n 는 에서 끝까지의 array요소 startIndex 수입니다.
추가 정보
적용 대상
IndexOf(Array, Object, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 요소 수에 대해 지정한 인덱스에서 확장됩니다.
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
검색할 1차원 배열입니다.
- value
- Object
array에서 찾을 개체입니다.
- startIndex
- Int32
검색할 시작 인덱스입니다. 0은 빈 배열에서 유효합니다.
- count
- Int32
검색할 요소의 수입니다.
반환
첫 번째 value 발생의 인덱스가 array의 인덱스 범위(startIndex ~ startIndex + count - 1)에 있으면 해당 인덱스이며 그렇지 않으면 배열의 하한에서 1을 뺀 값입니다.
예외
array이(가) null인 경우
startIndex가 array의 유효한 인덱스 범위를 벗어납니다.
또는
count가 0보다 작은 경우
또는
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.
설명
이 메서드는 가 0보다 큰 경우 count 1차원 배열의 요소를 에서 startIndex 빼기 startIndexcount 1로 검색합니다. 에 있는지 여부를 value 확인하려면 메서드는 일치 항목을 발견할 때까지 모든 요소의 메서드를 호출 Equals 하여 같음 비교를 수행array합니다. 즉, 요소가 메서드를 재정의하는 경우 해당 재정의 Object.Equals 가 호출됩니다.
대부분의 배열은 하한이 0이므로 이 메서드는 일반적으로 찾을 수 없는 경우 value -1을 반환합니다. 배열의 하한이 (0x80000000)와 같 Int32.MinValue 고 value 찾을 수 없는 드문 경우 이 메서드는 (0x7FFFFFFF)를 반환 Int32.MaxValue 합니다.
가 이Array.Length면 startindex 메서드는 -1을 반환합니다. 가 보다 Array.Length크면 startIndex 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다.
이 메서드는 O (n) 작업, 여기서 n 는 count합니다.
추가 정보
적용 대상
IndexOf<T>(T[], T, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 인덱스에서 배열의 끝까지 확장됩니다.
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[]
검색할 1차원(인덱스는 0부터 시작) 배열입니다.
- value
- T
array에서 찾을 개체입니다.
- startIndex
- Int32
검색의 0부터 시작하는 인덱스입니다. 0은 빈 배열에서 유효합니다.
반환
startIndex에서 마지막 요소까지 확장되는 array의 요소 범위 내에서 value가 있으면 처음으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -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
설명
이 메서드는 의 요소에서 배열의 끝까지 1 startIndex 차원 배열을 검색합니다. 에 있는지 여부를 value 확인하려면 메서드는 모든 요소에서 메서드를 호출하여 같음 비교를 T.Equals 수행array합니다. 즉, 메서드를 재정의하는 경우 T 해당 재정의 Equals 가 호출됩니다.
가 Length이면 startIndex 메서드는 -1을 반환합니다. 가 보다 Array.Length크면 startIndex 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다.
이 메서드는 O(n) 작업입니다. 여기서 n 는 에서 끝까지의 array요소 startIndex 수입니다.
추가 정보
적용 대상
IndexOf<T>(T[], T, Int32, Int32)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
1차원 배열의 요소 범위에서 지정한 개체를 검색하여 처음으로 일치하는 인덱스를 반환합니다. 범위는 지정한 요소 수에 대해 지정한 인덱스에서 확장됩니다.
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[]
검색할 1차원(인덱스는 0부터 시작) 배열입니다.
- value
- T
array에서 찾을 개체입니다.
- startIndex
- Int32
검색의 0부터 시작하는 인덱스입니다. 0은 빈 배열에서 유효합니다.
- count
- Int32
검색할 섹션에 있는 요소 수입니다.
반환
startIndex부터 시작하여 count에 지정된 수의 요소를 포함하는 array의 요소 범위 내에서 value가 있으면 처음으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -1입니다.
예외
array이(가) null인 경우
startIndex가 array의 유효한 인덱스 범위를 벗어납니다.
또는
count가 0보다 작은 경우
또는
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
설명
이 메서드는 가 0보다 큰 경우 count 1차원 배열의 요소를 에서 startIndex 빼기 startIndexcount 1로 검색합니다. 에 있는지 여부를 value 확인하려면 메서드는 모든 요소에서 메서드를 호출하여 같음 비교를 T.Equals 수행array합니다. 즉, 메서드를 재정의하는 경우 T 해당 재정의 Equals 가 호출됩니다.
가 이Array.Length면 startIndex 메서드는 -1을 반환합니다. 가 보다 Array.Length크면 startIndex 메서드는 을 ArgumentOutOfRangeExceptionthrow합니다.
이 메서드는 O (n) 작업, 여기서 n 는 count합니다.
추가 정보
적용 대상
IndexOf<T>(T[], T)
- Source:
- Array.cs
- Source:
- Array.cs
- Source:
- Array.cs
지정한 개체를 검색하여 1차원 배열에서 처음 검색된 개체의 인덱스를 반환합니다.
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[]
검색할 1차원(인덱스는 0부터 시작) 배열입니다.
- value
- T
array에서 찾을 개체입니다.
반환
전체 array에서 value가 있으면 처음으로 나타나는 개체의 인덱스(0부터 시작)이고, 그렇지 않으면 -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
설명
이 메서드는 에 대한 1차원 배열의 모든 요소를 검색합니다 value. 에 있는지 여부를 value 확인하려면 메서드는 모든 요소에서 메서드를 호출하여 같음 비교를 T.Equals 수행array합니다. 즉, 메서드를 재정의하는 경우 T 해당 재정의 Equals 가 호출됩니다.
이 메서드는 O(n) 작업이며 여기서 n 는 의 array입니다Length.