本文整理汇总了C#中System.Collections.ArrayList.Item属性的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.Item属性的具体用法?C# ArrayList.Item怎么用?C# ArrayList.Item使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.Item属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections;
public class Example
{
public static void Main()
{
// Create an empty ArrayList, and add some elements.
ArrayList stringList = new ArrayList();
stringList.Add("a");
stringList.Add("abc");
stringList.Add("abcdef");
stringList.Add("abcdefg");
// The Item property is an indexer, so the property name is
// not required.
Console.WriteLine("Element {0} is \"{1}\"", 2, stringList[2]);
// Assigning a value to the property changes the value of
// the indexed element.
stringList[2] = "abcd";
Console.WriteLine("Element {0} is \"{1}\"", 2, stringList[2]);
// Accessing an element outside the current element count
// causes an exception.
Console.WriteLine("Number of elements in the list: {0}",
stringList.Count);
try
{
Console.WriteLine("Element {0} is \"{1}\"",
stringList.Count, stringList[stringList.Count]);
}
catch(ArgumentOutOfRangeException aoore)
{
Console.WriteLine("stringList({0}) is out of range.",
stringList.Count);
}
// You cannot use the Item property to add new elements.
try
{
stringList[stringList.Count] = "42";
}
catch(ArgumentOutOfRangeException aoore)
{
Console.WriteLine("stringList({0}) is out of range.",
stringList.Count);
}
Console.WriteLine();
for (int i = 0; i < stringList.Count; i++)
{
Console.WriteLine("Element {0} is \"{1}\"", i,
stringList[i]);
}
Console.WriteLine();
foreach (object o in stringList)
{
Console.WriteLine(o);
}
}
}
输出:
Element 2 is "abcdef" Element 2 is "abcd" Number of elements in the list: 4 stringList(4) is out of range. stringList(4) is out of range. Element 0 is "a" Element 1 is "abc" Element 2 is "abcd" Element 3 is "abcdefg" a abc abcd abcdefg
示例2: Main
//引入命名空间
using System;
using System.Collections;
public class ScrambleList : ArrayList
{
public static void Main()
{
// Create an empty ArrayList, and add some elements.
ScrambleList integerList = new ScrambleList();
for (int i = 0; i < 10; i++)
{
integerList.Add(i);
}
Console.WriteLine("Ordered:\n");
foreach (int value in integerList)
{
Console.Write("{0}, ", value);
}
Console.WriteLine("<end>\n\nScrambled:\n");
// Scramble the order of the items in the list.
integerList.Scramble();
foreach (int value in integerList)
{
Console.Write("{0}, ", value);
}
Console.WriteLine("<end>\n");
}
public void Scramble()
{
int limit = this.Count;
int temp;
int swapindex;
Random rnd = new Random();
for (int i = 0; i < limit; i++)
{
// The Item property of ArrayList is the default indexer. Thus,
// this[i] is used instead of Item[i].
temp = (int)this[i];
swapindex = rnd.Next(0, limit - 1);
this[i] = this[swapindex];
this[swapindex] = temp;
}
}
}
输出:
Ordered: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,Scrambled: 5, 2, 8, 9, 6, 1, 7, 0, 4, 3,