【.NET基础加强第五课--泛型集合与非泛型集合】

.NET基础加强第五课--泛型集合与非泛型集合

    • 堆栈:后进先出 LIFO
    • 队列:先进先出 FIFO
    • ArrayList
    • 删除集合
    • 排序
    • 注意 没有降序,但可以用反转
    • ArrayList add 引用类型,不能直接sort
    • Hashtable
    • 注意
    • 练习:数字 小写转成大写
    • 字母出现 的次数
    • 练习:

堆栈:后进先出 LIFO

队列:先进先出 FIFO

ArrayList

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();

ArrayList add 引用类型,不能直接sort

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;
}

}

Hashtable

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 dictionary = new 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 map = new 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 item in map)
{
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 map = new 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();
}

}

你可能感兴趣的:(.net,基础加强,.net,开发语言,泛型集合与非泛型集合)