<span style="font-size:18px;">using System;
using System.Collections;//需要添加的命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 动态数组的使用
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个
Console.WriteLine("未添加前al的元素个数为:"+al.Count);
al.Add("abc");
al.Add("xyz");
al.Add("opq");
Console.WriteLine("调用Add方法后al的元素个数为:"+al.Count);
string[] last = { "def", "ghj" };
al.AddRange(last);
Console.WriteLine("调用AddRange方法后al的元素个数为:"+al.Count);
foreach (string item in al)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}</span>
<span style="font-size:18px;">using System;
using System.Collections;//需要添加的命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 动态数组的使用
{
class Program
{
static void Main(string[] args)
{
ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个
al.Add("abc");
al.Add(50);
al.Add(10);
string[] last = { "def", "ghj" };
al.AddRange(last);
Console.WriteLine("未删除前al的元素个数为:" + al.Count);
al.RemoveAt(2);//删除索引为2后的元素
Console.WriteLine("删除索引为2后的元素个数为:"+al.Count);
al.Remove("abc");//删除第一个值为abc的项
Console.WriteLine("删除值为abc后的元素个数为:"+al.Count);
al.RemoveRange(1,2);//删除自索引为1的两个元素
Console.WriteLine("删除自索引为1的两个元素后的元素个数:"+al.Count);
foreach (string item in al)//因为此对象中的元素类型不一致所以为object类型
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}</span>
<span style="font-size:18px;">using System;
using System.Collections;//需要添加的命名空间
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 动态数组的使用
{
class Program
{
static void Main(string[] args)
{
string[] str = { "a", "b", "c", "d", "d", "e", "f" };
ArrayList al = new ArrayList(str);
int i = al.IndexOf("c");//查找第一个字符c在数组中的位置
Console.WriteLine("元素c在集合中的位置是:"+i);
i = al.LastIndexOf("d");//查找最后一个字符d在数组中的位置
Console.WriteLine("元素d在集合中的位置是:" + i);
int j = al.BinarySearch("f");//查找元素f在数组中的位置
if (j>0)
{
Console.WriteLine("元素f在数组中的位置是:"+j);
}
else
{
Console.WriteLine("没有找到a");
}
Console.ReadLine();
}
}
}</span>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有