本文整理汇总了C#中System.Collections.ArrayList.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.ToArray方法的具体用法?C# ArrayList.ToArray怎么用?C# ArrayList.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.ToArray方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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( "jumps" );
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();
}
}
输出:
The ArrayList contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog The string array contains the following values: [0]: The [1]: quick [2]: brown [3]: fox [4]: jumps [5]: over [6]: the [7]: lazy [8]: dog
示例2: Main
//引入命名空间
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
// Create a new ArrayList and populate it.
ArrayList list = new ArrayList(5);
list.Add("B");
list.Add("G");
list.Add("J");
list.Add("S");
list.Add("M");
object[] array2 = list.ToArray();
Console.WriteLine("Array 2:");
foreach (string s in array2)
{
Console.WriteLine("\t{0}", s);
}
}
}
示例3: ArrayList.ToArray(typeof(T))
//引入命名空间
using System;
using System.Collections;
class MainClass
{
public static void Main(string[] args)
{
// Create a new ArrayList and populate it.
ArrayList list = new ArrayList(5);
list.Add("B");
list.Add("G");
list.Add("J");
list.Add("S");
list.Add("M");
string[] array3 = (string[])list.ToArray(typeof(String));
Console.WriteLine("Array 3:");
foreach (string s in array3)
{
Console.WriteLine("\t{0}", s);
}
}
}