using System.Collections;
ArrayList arrayList = new ArrayList();
arrayList.Add(0);
arrayList.Add(“张三”);
arrayList.Add(“李四”);
// 向指定的位置插入一个元素
arrayList.Insert(0, “yy”);
// 删除元素
arrayList.Remove(0);
arrayList.RemoveAt(1);
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
Console.WriteLine( " 元素个数 a r r a y L i s t . C o u n t " ) ; C o n s o l e . W r i t e L i n e ( "元素个数{arrayList.Count}"); Console.WriteLine( "元素个数arrayList.Count");Console.WriteLine(“容量:{arrayList.Capacity}”);
Console.ReadKey();
如下语句能删除掉集合吗?
for (int i = 0; i < arrayList.Count; i++)
{ arrayList.RemoveAt(i);
}
Console.WriteLine( " 元素个数 a r r a y L i s t . C o u n t " ) ; C o n s o l e . W r i t e L i n e ( "元素个数{arrayList.Count}"); Console.WriteLine( "元素个数arrayList.Count");Console.WriteLine(“容量:{arrayList.Capacity}”);
Console.ReadKey();
// 集合转为数组
arrayList.ToArray();
using System.Collections;
ArrayList arrayList = new ArrayList(
new int[] {1,33,55,23,8,9,80,29});
arrayList.Sort();
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
Console.ReadKey();
using System.Collections;
ArrayList arrayList = new ArrayList(
new int[] {1,33,55,23,8,9,80,29});
arrayList.Sort();
arrayList.Reverse();
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(arrayList[i]);
}
Console.ReadKey();
using System.Collections;
ArrayList arrayList = new ArrayList();
Person p1 = new Person();
p1.Name = “zs”;
p1.Age = 19;
Person p2 = new Person();
p1.Name = “ls”;
p1.Age = 20;
Person p3 = new Person();
p1.Name = “ww”;
p1.Age = 26;
Person p4 = new Person();
p1.Name = “zl”;
p1.Age = 18;
arrayList.Add(p1);
arrayList.Add(p2);
arrayList.Add(p3);
arrayList.Add(p4);
// 会报错
arrayList.Sort();
Console.ReadKey();
public class Person
{
public string Name { get; set; }
public int Id { get; set; }
public int Age { get; set; }
}
using System.Collections;
ArrayList arrayList = new ArrayList();
Person p1 = new Person();
p1.Name = “zs”;
p1.Age = 19;
Person p2 = new Person();
p2.Name = “ls”;
p2.Age = 20;
Person p3 = new Person();
p3.Name = “ww”;
p3.Age = 26;
Person p4 = new Person();
p4.Name = “zl”;
p4.Age = 18;
arrayList.Add(p1);
arrayList.Add(p2);
arrayList.Add(p3);
arrayList.Add(p4);
Console.WriteLine(arrayList.Count);
arrayList.Sort();
for (int i = 0; i < arrayList.Count; i++)
{
Console.WriteLine(((Person)arrayList[i]).Name);
}
Console.ReadKey();
public class Person:IComparable
{
public string Name { get; set; }
public int Id { get; set; }
public int Age { get; set; }
public int CompareTo(object? obj)
{
Person p = obj as Person;
if (p != null)
{
return p.Age - this.Age;
}
return 0;
}
}
using System.Collections;
Hashtable hashtable = new Hashtable();
hashtable.Add(“zs”,“张三”);
hashtable.Add(“ls”, new Person()
{
Name =“李四”,
Age = 19
});
//通过键获取值
Console.WriteLine(hashtable[“zs”].ToString());
Console.WriteLine(((Person)hashtable[“ls”]).Name.ToString());
Console.ReadKey();
public class Person:IComparable
{
public string Name { get; set; }
public int Id { get; set; }
public int Age { get; set; }
public int CompareTo(object? obj)
{
Person p = obj as Person;
if (p != null)
{
return p.Age - this.Age;
}
return 0;
}
}
键值对集合的键一定不能重复 (唯一)
using System.Text;
List list1 = new List { 1, 2, 3 ,88,22,77,99,33};
Dictionary
string str = “1壹 2贰 3参 4肆”;
string[] parts = str.Split(’ ');
for (int i = 0; i < parts.Length; i++)
{
dictionary.Add(parts[i][0], parts[i][1]);
}
Console.WriteLine(“请输入一个数值”);
string nums = Console.ReadLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < nums.Length; i++)
{
sb.Append(dictionary[nums[i]]);
}
Console.WriteLine(sb.ToString());
Console.ReadKey();
string msg = “YSTRING string NNN hello c# wellcome to china”;
Dictionary
for (int i = 0; i < msg.Length; i++)
{
if (char.IsLetter(msg[i]))
{
if (map.ContainsKey(char.ToLower(msg[i])))
{
map[char.ToLower(msg[i])]++;
}else if(map.ContainsKey(char.ToUpper(msg[i])))
{
map[char.ToUpper(msg[i])]++;
}
else
{
map[msg[i]] = 1;
}
}
}
foreach (KeyValuePair
{
Console.WriteLine($“字母{item.Key} 出现次数{item.Value}”);
}
Console.ReadKey();
日期由大写转成数字 ,例如:二零二二年十二月二十一日 转成 2022-12-21
string date = “二零二二年十二月二十一日”;
date = Mainse.ConvertDate(date);
Console.WriteLine(date);
Console.ReadKey();
public class Mainse
{
public static string ConvertDate(string date)
{
Dictionary
string stand = “零0 一1 二2 三3 四4 五5 六6 七7 八8 九9”;
string[] parts = stand.Split(’ ');
for (int i = 0; i < parts.Length; i++)
{
map.Add(parts[i][0],parts[i][1]);
}
StringBuilder sbDate = new StringBuilder();
for(int i = 0; i < date.Length; i++)
{
if(date[i] == '十')
{
// 特殊处理
if( !map.ContainsKey( date[i - 1]) && !map.ContainsKey(date[i + 1]))
{
sbDate.Append("10");
}else if(!map.ContainsKey(date[i - 1]) && map.ContainsKey(date[i + 1]))
{
sbDate.Append("1");
}
else if (map.ContainsKey(date[i - 1]) && map.ContainsKey(date[i + 1]))
{
}
else if (map.ContainsKey(date[i - 1]) && !map.ContainsKey(date[i + 1]))
{
sbDate.Append("0");
}
}
else if(map.ContainsKey(date[i]))
{
sbDate.Append(map[date[i]]);
}
else
{
sbDate.Append("-");
}
}
return sbDate.Remove(sbDate.Length - 1,1).ToString();
}
}