用 C# 获取或设置 ArrayList 可以包含的元素数
要获取或设置 ArrayList 可以包含的元素数,代码如下 −
示例
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add(25);
arrList.Add(50);
arrList.Add(75);
arrList.Add(100);
arrList.Add(125);
arrList.Add(150);
arrList.Add(175);
arrList.Add(200);
arrList.Add(225);
arrList.Add(250);
Console.WriteLine("Elements in ArrayList...");
foreach(int i in arrList) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements = " + arrList.Count);
Console.WriteLine("Capacity =" + arrList.Capacity);
}
}输出
这会产生以下输出 −
Elements in ArrayList... 25 50 75 100 125 150 175 200 225 250 Count of elements = 10 Capacity =16
示例
我们来看另一个示例 −
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList(10);
arrList.Add(25);
arrList.Add(50);
arrList.Add(75);
arrList.Add(100);
Console.WriteLine("Elements in ArrayList...");
foreach(int i in arrList) {
Console.WriteLine(i);
}
Console.WriteLine("Count of elements = " + arrList.Count);
Console.WriteLine("Capacity =" + arrList.Capacity);
}
}输出
这会产生以下输出 −
Elements in ArrayList... 25 50 75 100 Count of elements = 4 Capacity =10
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP