static void Main(string[] args)
{
List<string> list = new List<string>();
//向集合中添加一个值
list.Add("abc");
list.Add("dcf");
list.Add("aaa");
list.Add("bbb");
list.Add("ccc");
//List集合中值长度为多少
int i = list.Count;
Console.WriteLine(i);
//移除的是集合中第一个找到的匹配项
if (list.Remove("abc"))
{
Console.WriteLine("成功");
}
else
{
Console.WriteLine("失败");
}
//遍历集合中的每一个元素
foreach (string item in list)
{
Console.WriteLine(item);
}
//Contains集合中包含不包含一个值
if (list.Contains("abc"))
{
Console.WriteLine("有");
}
else
{
Console.WriteLine("没有");
}
//集合中的元素翻转
list.Reverse();
foreach (string item in list)
{
Console.WriteLine(item);
}
//将集合中的元素复制到新的数组中
string[] str = list.ToArray();
//增加元素的方法
list.Insert(0, "0");
foreach (string item in list)
{
Console.WriteLine(item);
}
List<int> list1 = new List<int>() { 1,2,3,4,5,6,7 };
List<string> list2 = new List<string>();
//得到一个名字的数组
string[] names = "a,b,c,d,e,f,g,h,i,j,k,l,m,n".Split(',');
//集合的遍历
List<string> nems = new List<string>();
for (int j = 0; j < names.Length; j++)
{
nems.Add(names[j]);
}
nems.RemoveAll(e => e == "l");
foreach (string item in nems)
{
Console.WriteLine(item);
}
//改元素的值
nems[3] = "BBBB";
//清空所有的元素
nems.Clear();
string kkk = "传说在传智播客.net部门有个讲师叫赵晓虎,赵晓虎很帅,很纯洁,赵晓虎是智慧和勇敢的化身.第一次听人这么说我不相信,于是我怀着试试看的心情来到了传智播客,见到了传说中的赵晓虎。哇塞,赵晓虎岂止是帅,简直就是帅的无法用言语来形容";
int index = -1;
do
{
index = kkk.IndexOf("赵晓虎", index + 1);
if (index != -1)
{
Console.WriteLine(index);
}
}
while (index != -1);
//第二种方法
List<int> num = new List<int>();
do
{
index = kkk.IndexOf("赵晓虎", index + 1);
if (index != -1)
{
num.Add(index);
}
} while (index != -1);
Console.WriteLine("=====================");
foreach (int item in num)
{
Console.WriteLine(item);
}
//第三种方法
Console.WriteLine("======================");
int[] ints = Search(kkk, "赵晓虎");
for (int s = 0; s < ints.Length; s++)
{
Console.WriteLine(ints[s]);
}
Console.ReadKey();
}
public static int[] Search(string s, string s1)
{
List<int> nums = new List<int>();
int index = -1;
do
{
index = s.IndexOf(s1, index + 1);
if (index != -1)
{
nums.Add(index);
}
} while (index!=-1);
return nums.ToArray();
}