如何迭代通过集合

如何迭代通过集合
本主题阐释如何创建和使用 ArrayList。本讨论的重要部分是如何使用 Foreach(VB 中为 For Each)命令依次通过列表。 ArrayListCollection 命名空间的成员,可接受任意类型的对象作为其元素。本主题还讲述 ArrayList 对象的方法和属性。

 
VB List.aspx

[ 运行示例] | [ 查看源代码]

集合非常类似于数组。总的来说,集合可视为特殊数组,带有更多方法和属性来帮助您解决问题。在本演示中,将使用一个特定集合 ArrayList

ArrayList 可接受各种对象,但当与集合一起使用时,尝试只使用一个数据类型是很有益的。您将查看的第一段示例代码是创建列表。这是一段简单的、只有一行的代码,在该代码中您使用 ArrayList 的构造函数生成该集合的新实例。与数组不同,没有必要了解列表将接受几个元素:ArrayList 可随着您向其添加元素而为您确定有几个元素。

<script language=JavaScript> function doClick(index, numTabs, id) { document.all("tab" + id, index).className = "tab"; for (var i=1; i < numTabs; i++) { document.all("tab" + id, (index + i) % numTabs).className = "backtab"; } document.all("code" + id, index).style.display = ""; for (var j=1; j < numTabs; j++) { document.all("code" + id, (index + j) % numTabs).style.display = "none"; } } </script>

// declare the list, with a default capacity of 16
ArrayList al = new ArrayList();
C# VB  

当生成 ArrayList 时,其内没有任何元素,但它最多能够接受 16 个元素。如果您试图添加第 17 个元素,ArrayList 将通过加倍其容量来帮助您解决问题。知道这一点很有用,因为以该方式扩大集合会伴随有某种性能瞬时干扰。如果知道集合将需要多少元素,则可以预先设置容量,以避免该性能问题(无论有多小)。

若要证明 ArrayList 的默认容量大于 16,需要向 ArrayList 询问其容量以及它有多少元素。可以通过访问列表中具有相同名称的属性来完成该操作。

// display the initial count, and capacity. 0 and 16
Console.WriteLine("The initial Count of our list is {0} " +
		and it's Capacity is {1}", al.Count, al.Capacity);
C# VB  

您自己练习它,以确保意见相合。可以设置容量属性,只要您设置的不低于当前列表中的元素数目且不低于零。计数是只读的,因为它反映列表中元素的实际数目,该数目由您添加的个数决定。

如果要向列表添加项,请使用 Add 方法,该方法接受任意对象作为参数,并将该对象放在列表的末尾。如果想将某对象放在列表中的特定位置,请使用 Insert 方法,指定要放置新对象位置处的索引以及要插入的对象。

不仅可以向列表添加元素,还可以从列表取走元素。若要这样做,请使用 Remove 方法,指定要移除的对象(您应使用对象引用来进行该操作),或使用 RemoveAt 方法,传递要移除元素的索引。下面的代码示例展示添加和移除对象。

// declare an Int32 object, to add to the list
Int32 intTemp = 12;

// add elements to the list
al.Add("Apple");
al.Add(intTemp);
al.Add( new MyType() ); // a specific object you made.

Console.WriteLine("Removing the integer object...");
// remove a specific element, using the Remove method
al.Remove(intTemp);

Console.WriteLine("Removing the MyType object...");
// remove a specific object, using RemoveAt
al.RemoveAt(2); // remove the MyType. Remember, we just removed intTemp,
		// which reduced all element indexes by 1.

Console.WriteLine("Inserting new object...");
// add an object at a specific location
al.Insert(0, "Grapes");

//display the count, and capacity
Console.WriteLine("After modifying our list, Count is {0} " +
		"and Capacity is {1}", al.Count, al.Capacity);
C# VB  

注意运行上面的代码后列表中留下的对象只有字符串。这将有助于编写 Foreach 语句。生成 ArrayList 后,可以通过生成一个临时对象来迭代通过该列表,此对象可依次指向列表的每个元素。临时的"指示"对象的数据类型可帮助您解决问题。由于只有字符串,所以可以使它为 String 对象。如果不确定,请使用一般对象。输出列表中的元素以后,请使用 Clear 方法移除列表中的所有元素。下面的代码示例展示了这些概念。

// note that because we know only Strings are left in our list,
// we can use a String object in our foreach statement. If you
// are at all uncertain, use foreach (Object item in al)


foreach (String item in al) {
	Console.WriteLine(item);
}

// remove all elements from the list
al.Clear();
C# VB  

记住 ArrayList 有许多其他有用的方法和属性可帮助您操作列表。请使用 .NET 框架 SDK 来确定这些元素的工作方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值