目录

ArrayList方法:

ArrayList添加对象:

Arraylist类:

ArrayList长度:

HashTable类:

Hashtable类练习:

IComparable泛型接口排序:

IComparable接口实现排序:

IComparer泛型接口排序:

List泛型集合:

List泛型集合练习:

var隐式类型:

泛型集合常用的扩展方法:

字典泛型集合:


ArrayList方法:

using System;
using System.Collections;namespace ConsoleApp1         //ArrayList方法
{class Program{static void Main(string[] args){ArrayList list = new ArrayList();//添加单个元素list.Add(1);list.Add("张三");list.Add(true);//添加集合或者数组list.AddRange(new int[] { 1, 2, 3, 4 });list.AddRange(list);//删除指定元素//list.Remove(true);删除指定下标元素//list.RemoveAt(0);//根据下标删除一定范围的元素// list.RemoveRange(0,2);//清除集合中的所有元素//list.Clear();//升序排序//list.Sort();//反转//list.Reverse();//在指定的位置插入一个元素// list.Insert(1,"插入的");//在指定位置插入一个数组// list.InsertRange(0,new string[] { "周一","周二"});//判断元素是否在这个集合中//bool b = list.Contains(1);//Console.WriteLine(b);//Console.WriteLine("---------------------");//if (!list.Contains("李腾"))//{//    list.Add("李腾");//}//else//{//    Console.WriteLine("已经有这个上课爱说话的学生了");//}for (int i = 0; i < list.Count; i++){Console.WriteLine(list[i]);}Console.ReadKey();}}
}

ArrayList添加对象:

using System;
using System.Collections;namespace ConsoleApp1        //ArrayList添加对象
{class Program{static void Main(string[] args){ArrayList list = new ArrayList();Student stu1 = new Student(1, "贾宝玉", 16, Gender.男);Student stu2 = new Student(2, "林黛玉", 14, Gender.女);Student stu3 = new Student(3, "薛宝钗", 15, Gender.女);list.Add(stu1);list.Add(stu2);list.Add(stu3);//按照索引取出对象Student s1 = (Student)list[1];s1.SayHi();Console.ReadKey();}}//学生类public class Student{public Student(int StuId, string Name, int Age, Gender Gender){this.stuId = StuId;this.name = Name;this.age = Age;this.gender = Gender;}public int stuId;public string name;public int age;public Gender gender;public void SayHi(){Console.WriteLine($"我是{this.name},今年{this.age}");}}public enum Gender{男,女}
}

Arraylist类:

using System;
using System.Collections;namespace ConsoleApp1        //Arraylist类
{class Program{static void Main(string[] args){//创建的ArrayList集合ArrayList list = new ArrayList();//集合:很多数据的集合//数组:长度不可变,类型单一//集合:长度可以任意改变,类型随便//添加元素list.Add(1);list.Add(3.14);list.Add(true);list.Add("张三");list.Add('男');list.Add(1000M);list.Add(new int[] { 1, 2, 3, 4, 5, 6 });// list.Add(list);for (int i = 0; i < list.Count; i++){if (list[i] is int[]){for (int j = 0; j < ((int[])list[i]).Length; j++){Console.WriteLine(((int[])list[i])[j]);}}else{Console.WriteLine(list[i]);}}Console.ReadKey();}}
}

ArrayList长度:

using System;
using System.Collections;namespace ConsoleApp1        //ArrayList长度
{/** count:实际集合中的元素个数* Capacity:可以包含的元素个数* 集合中实际包含的元素个数(count)超过了可以包含的元素个数(Capacity)的时候* 集合就会向内存中申请一倍的空间,来保证集合的长度一直够用* */class Program{static void Main(string[] args){ArrayList list = new ArrayList();list.Add(1);list.Add(1);list.Add(1);list.Add(1);list.Add(1);list.Add(1);list.Add(1);list.Add(1);list.Add(1);Console.WriteLine(list.Count);Console.WriteLine(list.Capacity);Console.ReadKey();}}
}

HashTable类:

using System;
using System.Collections;namespace ConsoleApp1        //HashTable类
{/** HashTable  键值对集合     字典    张     zhang ->张* 在键值对集合中,我们是根据键找值的* * */class Program{static void Main(string[] args){//创建一个键值对集合对象Hashtable ht = new Hashtable();ht.Add(1, "张三");ht.Add(2, true);ht.Add(3, '无');ht.Add(false, "错误的");ht[6] = "新来的";   //这也是一种添加数据的方式  ht[1] = "把张三干掉";//ContainsKey  是否包含这个键if (!ht.ContainsKey("888发发发")){ht.Add("888发发发", "李腾");}else{Console.WriteLine("已经有这个键了");}//清除所有元素//ht.Clear();// ht.Remove(6);//Console.WriteLine(ht[1]);//Console.WriteLine(ht[2]);//Console.WriteLine(ht[false]);//Console.WriteLine(ht[6]);//Console.WriteLine(ht["888发发发"]);foreach (var item in ht.Keys){Console.WriteLine("键是:{0}=================>值是{1}", item, ht[item]);// Console.WriteLine(item);}//for (int i = 0; i < ht.Count; i++)//{//    Console.WriteLine(ht[i]);//}Console.ReadKey();}}
}

Hashtable类练习:

using System;
using System.Collections;
using System.Text.RegularExpressions;namespace ConsoleApp1        //Hashtable类练习
{class Program{static void Main(string[] args){Hashtable ht = new Hashtable();while (true){//Console.BackgroundColor = ConsoleColor.Red;Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("=================请选择操作====================");Console.WriteLine("  1.添加联系人    2.查找    3.删除联系人   4. 修改联系人信息 ");Console.WriteLine("===============================================");Console.WriteLine("请输入你的选择");Console.ForegroundColor = ConsoleColor.Green;string input = Console.ReadLine();switch (input){case "1":Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("请输入联系人名字:");Console.ForegroundColor = ConsoleColor.Green;string name = Console.ReadLine();Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("请输入联系人手机号:");Console.ForegroundColor = ConsoleColor.Green;string tel = Console.ReadLine();//判断手机号是否合法bool b = Regex.IsMatch(tel, "^1[0-9]{10}$");if (b){ht.Add(name, tel);Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine($"*******共有{ht.Count}个联系人***********");}else{Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("您输入的手机号格式不对");}break;case "2":Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("已添加的联系人有:");foreach (var item in ht.Keys){Console.Write(item + " ");}Console.WriteLine();Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("请输入您要查找的联系人姓名:");Console.ForegroundColor = ConsoleColor.Green;string nameFind = Console.ReadLine();//获取这个联系人的电话var telFind = ht[nameFind];if (telFind == null){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("该联系人不存在");}else{Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine($"你查找的联系人的电话是{telFind}");}break;case "3":Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("已添加的联系人有:");foreach (var item in ht.Keys){Console.Write(item + " ");}Console.WriteLine();Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("请输入您想删除的联系人姓名:");Console.ForegroundColor = ConsoleColor.Green;string nameDel = Console.ReadLine();if (ht.ContainsKey(nameDel)){Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("您确定要删除吗,如果确定,请输入:y");Console.ForegroundColor = ConsoleColor.Green;string enterDel = Console.ReadLine();if (enterDel.Equals("y", StringComparison.OrdinalIgnoreCase)){ht.Remove(nameDel);Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("删除成功!");}}else{Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("对不起没有您要找的联系人");}break;case "4":Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("已添加的联系人有:");foreach (var item in ht.Keys){Console.Write(item + " ");}Console.WriteLine();Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine("请输入您想修改的联系人姓名:");Console.ForegroundColor = ConsoleColor.Green;string nameChange = Console.ReadLine();//获取这个联系人的电话var telChange = ht[nameChange];if (telChange == null){Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("该联系人不存在");}else{Console.ForegroundColor = ConsoleColor.Cyan;Console.WriteLine($"{nameChange}的手机号是{telChange},请输入新的手机号:");Console.ForegroundColor = ConsoleColor.Green;string telNew = Console.ReadLine();if (Regex.IsMatch(telNew, "^1[0-9]{10}$")){ht[nameChange] = telNew;Console.ForegroundColor = ConsoleColor.Yellow;Console.WriteLine("修改成功!");}else{Console.ForegroundColor = ConsoleColor.Red;Console.WriteLine("您输入的手机号格式不对");}}break;}}Console.ReadKey();}}
}

IComparable泛型接口排序:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //IComparable泛型接口排序
{class Program{static void Main(string[] args){Person p1 = new Person(1, "贾宝玉", 16);Person p2 = new Person(2, "林黛玉", 14);Person p3 = new Person(3, "薛宝钗", 15);Person P4 = new Person(4, "史湘云", 16);//创建泛型集合List<Person> list = new List<Person>() { p1, p2, p3, P4 };Console.WriteLine("排序前:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}list.Sort();Console.WriteLine("排序后:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}Console.ReadKey();}}public class Person : IComparable<Person>{public Person(int id, string name, int age){this.Name = name;this.Id = id;this.Age = age;}public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }public int CompareTo(Person other){return this.Age.CompareTo(other.Age);}}
}

IComparable接口实现排序:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //IComparable接口实现排序
{class Program{static void Main(string[] args){//List<string> list = new List<string>() {  "啊林黛玉", "比王熙凤","低贾宝玉" };//Console.WriteLine("排序前:");//foreach (string str in list)//{//    Console.WriteLine(str);//}//list.Sort();//Console.WriteLine("排序后:");//foreach (string str in list)//{//    Console.WriteLine(str);//}Person p1 = new Person(1, "贾宝玉", 16);Person p2 = new Person(2, "林黛玉", 14);Person p3 = new Person(3, "薛宝钗", 15);Person P4 = new Person(4, "史湘云", 16);//创建泛型集合List<Person> list = new List<Person>() { p1, p2, p3, P4 };Console.WriteLine("排序前:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}list.Sort();Console.WriteLine("排序后:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}Console.ReadKey();}}public class Person : IComparable{public Person(int id, string name, int age){this.Name = name;this.Id = id;this.Age = age;}public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }//实现接口方法public int CompareTo(object obj){//把obj转换成Person类型的对象,如果转换失败,则是NullPerson per = obj as Person;//把当前对象的name成员和接受的参数的name成员进行比较,返回结果// 返回类型是int类型,返回大于0 表示,当前对象大于per,小于,则相反return this.Name.CompareTo(per.Name);}}
}

IComparer泛型接口排序:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //IComparer泛型接口排序
{class Program{static void Main(string[] args){Person p1 = new Person(1, "贾宝玉", 16);Person p2 = new Person(2, "林黛玉", 14);Person p3 = new Person(3, "薛宝钗", 15);Person P4 = new Person(4, "史湘云", 16);//创建泛型集合List<Person> list = new List<Person>() { p1, p2, p3, P4 };Console.WriteLine("排序前:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}Console.WriteLine("请选择:1.按姓名排序  2.按年龄排序");string select = Console.ReadLine();switch (select){case "1":list.Sort(new NameSort());break;case "2":list.Sort(new AgeSort());break;}Console.WriteLine("排序后:");foreach (var per in list){Console.WriteLine($"姓名{per.Name},年龄{per.Age}");}Console.ReadKey();}}public class Person{public Person(int id, string name, int age){this.Name = name;this.Id = id;this.Age = age;}public int Id { get; set; }public string Name { get; set; }public int Age { get; set; }}//按姓名排序public class NameSort : IComparer<Person>{public int Compare(Person x, Person y){return x.Name.CompareTo(y.Name);}}//按年龄排序public class AgeSort : IComparer<Person>{public int Compare(Person x, Person y){return x.Age.CompareTo(y.Age);}}
}

List泛型集合:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //List泛型集合
{/** 泛型集合就是明确指定了要放入集合的对象是何种类型的集合* */class Program{static void Main(string[] args){//创建List泛型集合的对象List<int> list = new List<int>();list.Add(1);list.Add(2);list.Add(3);list.AddRange(new int[] { 4, 5, 6 });list.AddRange(list);for (int i = 0; i < list.Count; i++){Console.WriteLine(list[i]);}Console.ReadKey();}}
}

List泛型集合练习:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //List泛型集合练习
{class Program{static void Main(string[] args){Student stu1 = new Student(1, "曹凯军");Student stu2 = new Student(2, "何帅");Student stu3 = new Student(3, "李琴琴");//集合初始化器List<Student> students = new List<Student>() { stu1, stu2, stu3 };//foreach (Student item in students)//{//    Console.WriteLine(item.Say());//}//ForEach()方法结合lambda表达式实现循环遍历// students.ForEach(s=>Console.WriteLine(s.Say()));//FindAll方法检索条件匹配所有元素List<Student> _students = students.FindAll(m => m.Id < 2);_students.ForEach(s => Console.WriteLine(s.Say()));Console.ReadKey();}}public class Student{//private int id;//public int Id//{//    get { return id; }//    set { id = value; }//}public Student(int id, string name){this.Id = id;this.Name = name;}//自动属性public int Id { get; set; }public string Name { get; set; }public string Say(){return "姓名:" + Name + ",学号:" + Id;}}
}

var隐式类型:

using System;namespace ConsoleApp1        //var隐式类型
{class Program{static void Main(string[] args){//int i = 1;//string s = "张三";var i = 2;var str = "李四";Console.WriteLine(i.GetType());Console.WriteLine(str.GetType());Console.ReadKey();}}
}

泛型集合常用的扩展方法:

using System;
using System.Collections.Generic;
using System.Linq;namespace ConsoleApp1        //泛型集合常用的扩展方法
{class Program{static void Main(string[] args){Person p1 = new Person(1, "贾宝玉");Person p2 = new Person(2, "林黛玉");Person p3 = new Person(3, "薛宝钗");Person P4 = new Person(4, "史湘云");//创建泛型集合List<Person> persons = new List<Person>() { p1, p2, p3, P4 };//获取泛型集合中第一个元素,如果没有,则返回null//需引用System.Linq命名空间Person per1 = persons.FirstOrDefault();Console.WriteLine($"泛型集合中第一个元素的名字是{per1.Name}");//判断泛型集合中是不是所有的元素ID小于3,如果是返回truebool b = persons.All(s => s.Id > 0);Console.WriteLine(b);//通过where()扩展方法,过滤id<3的person对象,Count就是符合条件的对象个数int perCount = persons.Where(s => s.Id < 3).Count();Console.WriteLine(perCount);Console.ReadKey();}}public class Person{public Person(int id, string name){this.Name = name;this.Id = id;}public int Id { get; set; }public string Name { get; set; }}
}

字典泛型集合:

using System;
using System.Collections.Generic;namespace ConsoleApp1        //字典泛型集合
{class Program{static void Main(string[] args){Dictionary<int, string> dic = new Dictionary<int, string>();dic.Add(1, "曹凯军");dic.Add(2, "何帅");dic.Add(3, "李琴琴");//foreach (var item in dic.Keys)//{//    Console.WriteLine("{0}=========>{1}",item,dic[item]);//}foreach (KeyValuePair<int, string> kv in dic){Console.WriteLine("{0}=>{1}", kv.Key, kv.Value);}Console.ReadKey();}}
}

Lession11 集合和泛型(ArrayList方法、Arraylist类、ArrayList添加对象、ArrayList长度、HashTable类、Hashtable类练习-----)相关推荐

  1. JSON 泛型序列化方法 与 LinkedHashMap转成对象

    JSON 泛型序列化方法 与 LinkedHashMap转成对象 1.说明 1.JSON 泛型序列化方法 2.1 JSON 源码 2.2 示例 2.3 忽略反转义报错 3.LinkedHashMap ...

  2. TP的依赖注入:将类类型的对象作为参数注入到当前类中

    思想 依赖注入其实本质上是指对类的依赖通过构造器完成自动注入, 例如在控制器架构方法和操作方法中一旦对参数进行对象类型约束则会自动触发依赖注入, 由于访问控制器的参数都来自于URL请求,普通变量就是通 ...

  3. Python OOP:面向对象基础,定义类,创建对象/实例,self,创建多个对象,添加对象属性,访问对象属性,__init__方法,带参数的__init__,__str__方法,__del__方法

    一.理解面向对象 面向对象是⼀种抽象化的编程思想,很多编程语⾔中都有的⼀种思想. ⾯向对象就是将编程当成是⼀个事物,对外界来说,事物是直接使用的,不用去管他内部的情况.⽽编程就是设置事物能够做什么事. ...

  4. java.lang.class对象_Java反射——java.lang.Class 类简介

    Java的基本思想之一是万事万物即对象,类也是一种对象.但是类是什么对象呢?Java中的类是java.lang.Class的实例化对象,这被成为类类型. //java.lang.Class类中的的主要 ...

  5. Class类 和 class对象(运行时的类型信息)

    什么是类?可以理解为.class文件 某种意义上来说,java有两种对象:实例对象和Class对象.每个类的运行时的类型信息就是用Class对象表示的.它包含了与类有关的信息.其实我们的实例对象就通过 ...

  6. 【设计模式】适配器模式 ( 类适配器代码模板 | 对象适配器代码模板 | 适配器模式示例 )

    文章目录 I . 适配器模式 ( 类适配器 ) 代码模板 II . 适配器模式 ( 对象适配器 ) 代码模板 III . 适配器模式 代码示例 I . 适配器模式 ( 类适配器 ) 代码模板 1 . ...

  7. scala案例_Scala案例类和案例对象深入(第1部分)

    scala案例 发表简短目录 (Post Brief TOC) Introduction介绍 What is Case Class什么是案例类 What is Case Object什么是案例对象 S ...

  8. [css] CSS的伪类和伪对象有什么不同?

    [css] CSS的伪类和伪对象有什么不同? 伪类是给当前选中节点添加新样式, 伪对象是给当前选中节点添加伪元素. 伪类选择器使用:,伪对象选择器使用::,因为兼容旧版,所以伪对象使用:也能解析. 个 ...

  9. 14 Java集合(集合框架+泛型+ArrayList类+LinkedList类+Vector类+HashSet类等)

    本篇主要是集合框架基础和List集合,Map集合等等后续更 集合 14.1 集合框架 14.1.1 概念 14.1.2 集合架构 14.2 Collection接口 14.2.1 常用方法 14.3 ...

最新文章

  1. python structure_GitHub - CYZYZG/Data_Structure_with_Python: 这是我在学习《基于Python的数据结构》的时候的笔记与代码...
  2. 定制化Azure站点Java运行环境(1)
  3. 4.mysql数据库创建,表中创建模具模板脚本,mysql_SQL99标准连接查询(恩,外部连接,全外连接,交叉连接)...
  4. oracle10g 创建分区表,oracle10G分区的创建与维护Oracle分区表和本地索引
  5. Android协程学习
  6. 无人机图像处理工具更新——多线程优化版
  7. mysql客户库_你应该知道的10个MySQL客户启动选项
  8. 【重难点】【JUC 02】volitale 常用模式 、JUC 下有哪些内容 、并发工具类
  9. ServerProperties
  10. 【Java】多线程编程
  11. VS2012 VS2010 VTK引入设置
  12. kb888111音频补丁FOR XP SP2
  13. 【HTML+CSS】自定义字体
  14. BEC高级商务英语考试应试技巧指南
  15. git在回退版本时HEAD~和HEAD^的作用和区别
  16. 华为部分通知气泡显示_华为P50已在路上,目前不受美国影响 | PS5价格曝光!
  17. windbg分析C++ EH exception
  18. 【C语言练习——打印上三角及其变形(带空格版)】
  19. 头插法和尾插法总结(动图版)
  20. 【目标检测算法】YOLO-V5实战检测VOC2007数据集

热门文章

  1. 导出word功能,用html代码在word中插入分页符
  2. JavaScript实现公历转换农历
  3. MySQL比较两张表数据相同、不同结果记录
  4. 2022电赛省一-小车跟随行驶系统(C题)
  5. 日本地震波及芯片产业链致价格走势难料
  6. 腾讯 Techo Hub 2022 年首站落地福州|723,与开发者们探讨工业数字化!
  7. Python使用天气网api接口获取天气数据
  8. 计算机认知训练效果,为轻度认知功能障碍的人保持认知功能而进行的计算机化认知训练...
  9. LT8911EXB MIPI CSI/DSI转EDP信号转换芯片
  10. 做软件产品有哪种商业模式?