using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 定义可变数据组类型 List<Dog> list = new List<Dog>(); // 添加数组元素 list.Add(new Dog("A")); list.Add(new Dog("B")); list.Add(new Dog("C")); list.Add(new Dog("D")); list.Add(new Dog("E")); list.RemoveAt(1); // 删除第二个,1表示的是下标 for (int i = 0; i < list.Count; ++i) { list[i].ShowName(); } } public class Dog { private string Name; public Dog(string name) { Name = name; } public void ShowName() { Console.WriteLine("狗的名字是:" + Name); } } } }
字典的使用形式:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 字典的定义 Dictionary<string, Dog> dog1 = new Dictionary<string, Dog>(); // 添加形式 dog1.Add("A", new Dog("A")); dog1.Add("B", new Dog("B")); dog1.Add("C", new Dog("C")); dog1.Add("D", new Dog("D")); // 访问形式 dog1["B"].ShowName(); } public class Dog { private string Name; public Dog(string name) { Name = name; } public void ShowName() { Console.WriteLine("狗的名字是:" + Name); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 调用栈 Stack<Dog> stack1 = new Stack<Dog>(); // push进栈操作 stack1.Push(new Dog("A")); stack1.Push(new Dog("B")); stack1.Push(new Dog("C")); // peek() 查看当前栈顶元素 stack1.Peek().ShowName(); // 显示"C" // 弹出栈顶指针 stack1.Pop(); // 弹出"C" stack1.Peek().ShowName(); // 此时应该显示"B",因为"C"已经被pop()弹出 } public class Dog { private string Name; public Dog(string name) { Name = name; } public void ShowName() { Console.WriteLine("狗的名字是:" + Name); } } } }
队列的使用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // 队列的使用 Queue<Dog> dog1 = new Queue<Dog>(); // 进队操作 dog1.Enqueue(new Dog("A")); dog1.Enqueue(new Dog("B")); dog1.Enqueue(new Dog("C")); // 出队列操作 dog1.Dequeue().ShowName(); // 先进先出,所以弹出的是"A" } public class Dog { private string Name; public Dog(string name) { Name = name; } public void ShowName() { Console.WriteLine("狗的名字是:" + Name); } } } }
栈的使用