BinarySearch (Object) 方法
使用默认的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。
参数
-
value
-
类型:
System.Object
要定位的 Object。该值可以为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
返回值
类型: System.Int32如果找到 value,则为已排序的 ArrayList 中从零开始的 value 索引;否则为一个负数,它是大于 value 的下一个元素索引的按位求补,如果没有更大的元素,则为 Count 的按位求补。
BinarySearch 方法 (Object, IComparer)
使用指定的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。
参数
-
value
-
类型:
System.Object
要定位的 Object。该值可以为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
-
comparer
-
类型:
System.Collections.IComparer
比较元素时要使用的 IComparer 实现。
- 或 -
nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 表示使用默认比较器,即每个元素的 IComparable 实现。
返回值
类型: System.Int32如果找到 value,则为已排序的 ArrayList 中从零开始的 value 索引;否则为一个负数,它是大于 value 的下一个元素索引的按位求补,如果没有更大的元素,则为 Count 的按位求补。
BinarySearch 方法 (Int32, Int32, Object, IComparer)
使用指定的比较器在已排序 ArrayList 的某个元素范围中搜索元素,并返回该元素从零开始的索引。
参数
-
index
-
类型:
System.Int32
要搜索的范围从零开始的起始索引。
-
count
-
类型:
System.Int32
要搜索的范围的长度。
-
value
-
类型:
System.Object
要定位的 Object。该值可以为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
-
comparer
-
类型:
System.Collections.IComparer
比较元素时要使用的 IComparer 实现。
- 或 -
nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 表示使用默认比较器,即每个元素的 IComparable 实现。
返回值
类型: System.Int32如果找到 value,则为已排序的 ArrayList 中从零开始的 value 索引;否则为一个负数,它是大于 value 的下一个元素索引的按位求补,如果没有更大的元素,则为 Count 的按位求补。
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. BinarySearch requires // a sorted ArrayList. ArrayList myAL = new ArrayList(); for ( int i = 0; i <= 4; i++ ) myAL.Add( i*2 ); // Displays the ArrayList. Console.WriteLine( "The Int32 ArrayList contains the following:" ); PrintValues( myAL ); // Locates a specific object that does not exist in the ArrayList. Object myObjectOdd = 3; FindMyObject( myAL, myObjectOdd ); // Locates an object that exists in the ArrayList. Object myObjectEven = 6; FindMyObject( myAL, myObjectEven ); } public static void FindMyObject( ArrayList myList, Object myObject ) { int myIndex=myList.BinarySearch( myObject ); if ( myIndex < 0 ) Console.WriteLine( "The object to search for ({0}) is not found. The next larger object is at index {1}.", myObject, ~myIndex ); else Console.WriteLine( "The object to search for ({0}) is at index {1}.", myObject, myIndex ); } public static void PrintValues( IEnumerable myList ) { foreach ( Object obj in myList ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. The Int32 ArrayList contains the following: 0 2 4 6 8 The object to search for (3) is not found. The next larger object is at index 2. The object to search for (6) is at index 3. */
CopyTo 方法 (Int32, Array, Int32, Int32)
从目标数组的指定索引处开始,将一定范围的元素从 ArrayList 复制到兼容的一维 Array 中
-
参数
index
-
类型:
System.Int32
源 ArrayList 中复制开始位置的从零开始的索引。
-
array
-
类型:
System.Array
-
arrayIndex
-
类型:
System.Int32
array 中从零开始的索引,在此处开始复制。
-
count
-
类型:
System.Int32
要复制的元素数。
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes the source ArrayList.
ArrayList mySourceList = new ArrayList();
mySourceList.Add( "three" );
mySourceList.Add( "napping" );
mySourceList.Add( "cats" );
mySourceList.Add( "in" );
mySourceList.Add( "the" );
mySourceList.Add( "barn" );
// Creates and initializes the one-dimensional target Array.
String[] myTargetArray = new String[15];
myTargetArray[0] = "The";
myTargetArray[1] = "quick";
myTargetArray[2] = "brown";
myTargetArray[3] = "fox";
myTargetArray[4] = "jumped";
myTargetArray[5] = "over";
myTargetArray[6] = "the";
myTargetArray[7] = "lazy";
myTargetArray[8] = "dog";
// Displays the values of the target Array.
Console.WriteLine( "The target Array contains the following (before and after copying):" );
PrintValues( myTargetArray, ' ' );
// Copies the second element from the source ArrayList to the target Array, starting at index 7.
mySourceList.CopyTo( 1, myTargetArray, 7, 1 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 6.
mySourceList.CopyTo( myTargetArray, 6 );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
// Copies the entire source ArrayList to the target Array, starting at index 0.
mySourceList.CopyTo( myTargetArray );
// Displays the values of the target Array.
PrintValues( myTargetArray, ' ' );
}
public static void PrintValues( String[] myArr, char mySeparator ) {
for ( int i = 0; i < myArr.Length; i++ )
Console.Write( "{0}{1}", mySeparator, myArr[i] );
Console.WriteLine();
}
}
/*
This code produces the following output.
The target Array contains the following (before and after copying):
The quick brown fox jumped over the lazy dog
The quick brown fox jumped over the napping dog
The quick brown fox jumped over three napping cats in the barn
three napping cats in the barn three napping cats in the barn
*/
GetEnumerator 方法
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/3112b7b6526db5bc83e275260ae60525.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/eec4c0236afc26744c9c4e910bc34958.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/4fd96b3cf02f4c7b5c8964ac8167f7af.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f0cd6c7f9e7ae96feae062cb48f670f0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/587e34b10dcf5efbc0859b53470a2db3.gif)
返回值
类型: System.Collections.ArrayListArrayList,它表示源 ArrayList 中元素的子集。
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Creates and initializes the source ICollection. Queue mySourceList = new Queue(); mySourceList.Enqueue( "big" ); mySourceList.Enqueue( "gray" ); mySourceList.Enqueue( "wolf" ); // Displays the values of five elements starting at index 0. ArrayList mySubAL = myAL.GetRange( 0, 5 ); Console.WriteLine( "Index 0 through 4 contains:" ); PrintValues( mySubAL, '/t' ); // Replaces the values of five elements starting at index 1 with the values in the ICollection. myAL.SetRange( 1, mySourceList ); // Displays the values of five elements starting at index 0. mySubAL = myAL.GetRange( 0, 5 ); Console.WriteLine( "Index 0 through 4 now contains:" ); PrintValues( mySubAL, '/t' ); } public static void PrintValues( IEnumerable myList, char mySeparator ) { foreach ( Object obj in myList ) Console.Write( "{0}{1}", mySeparator, obj ); Console.WriteLine(); } } /* This code produces the following output. Index 0 through 4 contains: The quick brown fox jumped Index 0 through 4 now contains: The big gray wolf jumped */ Repeat 方法
返回 ArrayList,它的元素是指定值的副本。
参数
value
- 类型: System.Object
要在新 ArrayList 中对其进行多次复制的 Object。该值可以为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
count
- 类型: System.Int32
value 应被复制的次数。
返回值
具有 count 所指定的元素数的 ArrayList,其中的所有元素都是 value 的副本。
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates a new ArrayList with five elements and initialize each element with a null value. ArrayList myAL = ArrayList.Repeat( null, 5 ); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "ArrayList with five elements with a null value" ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); // Creates a new ArrayList with seven elements and initialize each element with the string "abc". myAL = ArrayList.Repeat( "abc", 7 ); // Displays the count, capacity and values of the ArrayList. Console.WriteLine( "ArrayList with seven elements with a string value" ); Console.WriteLine( " Count : {0}", myAL.Count ); Console.WriteLine( " Capacity : {0}", myAL.Capacity ); Console.Write( " Values:" ); PrintValues( myAL ); } public static void PrintValues( IEnumerable myList ) { foreach ( Object obj in myList ) Console.Write( " {0}", obj ); Console.WriteLine(); } } /* This code produces the following output. ArrayList with five elements with a null value Count : 5 Capacity : 16 Values: ArrayList with seven elements with a string value Count : 7 Capacity : 16 Values: abc abc abc abc abc abc abc */
Sort 方法 (IComparer)
使用指定的比较器对整个 ArrayList 中的元素进行排序。
参数
comparer
- 类型: System.Collections.IComparer
比较元素时要使用的 IComparer 实现。
- 或 -
若为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则使用每个元素的 IComparable 实现。
using System; using System.Collections; public class SamplesArrayList { public class myReverserClass : IComparer { // Calls CaseInsensitiveComparer.Compare with the parameters reversed. int IComparer.Compare( Object x, Object y ) { return( (new CaseInsensitiveComparer()).Compare( y, x ) ); } } public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumps" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Displays the values of the ArrayList. Console.WriteLine( "The ArrayList initially contains the following values:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the default comparer. myAL.Sort(); Console.WriteLine( "After sorting with the default comparer:" ); PrintIndexAndValues( myAL ); // Sorts the values of the ArrayList using the reverse case-insensitive comparer. IComparer myComparer = new myReverserClass(); myAL.Sort( myComparer ); Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" ); PrintIndexAndValues( myAL ); } public static void PrintIndexAndValues( IEnumerable myList ) { int i = 0; foreach ( Object obj in myList ) Console.WriteLine( "/t[{0}]:/t{1}", i++, obj ); Console.WriteLine(); } } /* This code produces the following output. The ArrayList initially contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog After sorting with the default comparer: [0]: brown [1]: dog [2]: fox [3]: jumps [4]: lazy [5]: over [6]: quick [7]: the [8]: The After sorting with the reverse case-insensitive comparer: [0]: the [1]: The [2]: quick [3]: over [4]: lazy [5]: jumps [6]: fox [7]: dog [8]: brown */
ToArray 方法 (Type)
将 ArrayList 的元素复制到指定元素类型的新数组中。
参数
type
- 类型: System.Type
要创建并向其复制元素的目标数组的元素 Type。
返回值
类型: System.Array
指定元素类型的数组,它包含 ArrayList 中元素的副本。
using System; using System.Collections; public class SamplesArrayList { public static void Main() { // Creates and initializes a new ArrayList. ArrayList myAL = new ArrayList(); myAL.Add( "The" ); myAL.Add( "quick" ); myAL.Add( "brown" ); myAL.Add( "fox" ); myAL.Add( "jumped" ); myAL.Add( "over" ); myAL.Add( "the" ); myAL.Add( "lazy" ); myAL.Add( "dog" ); // Displays the values of the ArrayList. Console.WriteLine( "The ArrayList contains the following values:" ); PrintIndexAndValues( myAL ); // Copies the elements of the ArrayList to a string array. String[] myArr = (String[]) myAL.ToArray( typeof( string ) ); // Displays the contents of the string array. Console.WriteLine( "The string array contains the following values:" ); PrintIndexAndValues( myArr ); } public static void PrintIndexAndValues( ArrayList myList ) { int i = 0; foreach ( Object o in myList ) Console.WriteLine( "/t[{0}]:/t{1}", i++, o ); Console.WriteLine(); } public static void PrintIndexAndValues( String[] myArr ) { for ( int i = 0; i < myArr.Length; i++ ) Console.WriteLine( "/t[{0}]:/t{1}", i, myArr[i] ); Console.WriteLine(); } } /* This code produces the following output. The ArrayList contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog The string array contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumped [5]: over [6]: the [7]: lazy [8]: dog */