数组增删改查
动态数组的增
格式 实例化的变量名 Add(添加的内容)
动态数组的删
格式 实例化的变量名 Remove(删除的内容)
利用索引下标来删除
格式 实例化的变量名 RemoveAt(删除的内容)
动态数组的改
格式 数组[索引] = 值
动态数组的查
for(int i = 0;i<数组.Count;i++){
Console.WriteLine(数组[ i ]);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 动态数组的增删改查
{
class Program
{
static void Main(string[] args)
{
//西游记取经团队的人员变化
//团队成立 唐僧是队长 成员数量是1 动态数组的创建
ArrayList team = new ArrayList(){"唐僧"};
//经历五行山 新加入成员孙悟空 成员数量是2
team.Add("孙悟空");
Console.WriteLine("当前团队人数为"+team.Count);
//经历高老庄 新加入成员猪八戒
team.Add("猪八戒");
//经历流沙河 新加入成员白龙马和沙僧
team.Add("白龙马");
team.Add("沙僧");
Console.WriteLine("当前团队人数为"+team.Count);
//盘丝洞 唐僧丢失 成员数量减少
team.Remove("唐僧");
Console.WriteLine("当前团队人数为"+team.Count);
//唐僧被救回来了 成员数量增加
team.Add("唐僧");
//女儿国唐僧又丢了 这次用索引值来删除数据
team.RemoveAt(4);
//回来
team.Add("唐僧");
//当前团队人数
Console.WriteLine("当前团队人数为" + team.Count);
//修改数据 修改成四人组成佛后的佛号
//数组[索引]=值
team[0] = "斗战胜佛";
team[1] = "净坛使者";
team[2] = "八部天龙";
team[3] = "金身罗汉";
team[4] = "旃檀功德佛";
//这个用法:
//foreach(数据类型 变量名 in 实例化的变量名)
//最后直接输出变量名
foreach (object item in team){
Console.WriteLine(item);
}
//区分线
Console.WriteLine("------------------------");
//for循环
//用team的长度来循环
//最后取team的索引值输出
for(int i = 0;i<team.Count;i++){
Console.WriteLine(team[i]);
}
Console.ReadLine();
}
}
}