个人主页:@元宇宙-秩沅
hallo 欢迎 点赞 收藏⭐ 留言 加关注✅!
本文由 秩沅 原创
收录于专栏:unity之c#专题篇
总结数组,list,Dectionary,Stack,Queue,LinkedList等存储容器。我们怎么来使用
数组:简单的数据类型存储的时候,或者只需要查改数据的时候
List:它是ArraryList的泛型升级,适合一切对象的存储,适合查改的情况下使用
LinkeList:它是泛型双向链表,适合频繁增删的数据对象的情况下使用
Dectionary:它是Hashtable的泛型升级,适合键值对象的存储
Stack:适合先进后出的情况下使用
Queue:适合先进先出的情况下使用
///
/// 13.List的删除和存取
///
//class Program
// {
//List和ArrayList的区别:前者结合了泛型,避免了拆箱装箱,效率更加优化
//static void Main(string[] args)
//{
// List text = new List();
// int[] arrary = new int[10]{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
// int i = 0;
// while (i
// {
// text.Add(arrary[i++]);
// }
// text.RemoveAt(5);
// foreach (int item in text)
// {
// Console.WriteLine(item);
// }
//}
//}
///在构造函数中用List存取元素
class Monster
{
static public List<Monster> control = new List<Monster>();
public Monster()
{
control.Add(this);
}
virtual public void attack() { }
}
class Boss : Monster
{
public override void attack()
{
Console.WriteLine("放大招");
}
}
class Gablin : Monster
{
public override void attack()
{
Console.WriteLine("哥布林的技能");
}
}
class Program
{
static void Main(string[] args)
{
Boss boss1 = new Boss();
Gablin boss2 = new Gablin();
Monster text = new Monster();
foreach (Monster item in Monster .control )
{
item.attack();
}
}
}
private Dictionary<int, string> text = new Dictionary<int, string>();
public Program()
{
string[] upperArrary = { "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾" };
for (int i = 0; i < upperArrary.Length; i++)
{
text.Add(i + 1, upperArrary[i]);
}
}
public void Upper( string num )
{
for (int i = 0; i < num.Length; i++)
{
int index = num[i] - '0';
Console.Write(text[index]);
}
}
static void Main(string[] args)
{
Program Exam = new Program();
Console.WriteLine(Exam.text[3]);
try
{
Console.WriteLine("请输入一个不超过三位数的整数");
int num = int.Parse(Console.ReadLine());
Exam.Upper(num.ToString());
}
catch (Exception)
{
throw;
}
}
Dictionary<char, int> text = new Dictionary<char, int>();
public void Multiplay(string arrray )
{
for (int i = 0; i < arrray.Length; i++)
{
if(text.ContainsKey(arrray[i]))
{
text[arrray[i]] += 1;
}
else
{
text.Add(arrray[i],1);
}
}
}
public void Print()
{
foreach (KeyValuePair<char ,int> item in text)
{
Console.WriteLine(item);
}
}
static void Main(string[] args)
{
Program text = new Program();
string arrary = "Welcome to Unity World";
string Arrary = arrary.ToUpper();
text.Multiplay(Arrary);
text.Print();
}
//1.
//顺序存储是一组连续的存储单元依次存储在线性表中的存储 方式(连续存储)
//链式存储是将一组任意不连续的存储单元存储在线性表中存储方式(任意存储)
//2.
//顺序存储的查改效率大于链式存储
//链式存储的增删效率大于顺序存储
//3.
//常用的数据结构有:数组,链表,栈,队列,数,图,堆,散列表
class LinkedNode<T>
{
public T vaule;
public LinkedNode(T Vaule)
{
this.vaule = Vaule;
}
public LinkedNode<T> peakLink = null;
public LinkedNode<T> nextLink = null;
}
class Link<T>
{
private int count = 0;
private LinkedNode<T> head;
private LinkedNode<T> last;
public int Count { get => count; }
public LinkedNode<T> Peak { get => head; }
public LinkedNode<T> Last { get => last; }
public void Add(LinkedNode<T> node) //添加节点
{
if (head == null)
{
head = node;
last = node;
count++;
}
else
{
//尾插法
LinkedNode<T> temp = last; ;
last.nextLink = node;
last = node;
last.peakLink = temp;
count++;
}
}
public void RemoveAt(int index) //删除节点
{
LinkedNode<T> Lnode = head ;
int temp = 1;
if (index > count || index < 1)
{
Console.WriteLine("超出链表规定范围,请输入正确范围进行移除操作!");
return;
}
else if (index == 1)
{
Console.WriteLine("指令为删除头节点");
head = head.nextLink;
}
else if (index == count)
{
Console.WriteLine("指令为删除尾节点");
last = last.peakLink;
Console.WriteLine("此时尾节点为:" + last.vaule);
last.nextLink = null;
}
else
{
while (true)
{
if (temp == index)
{
if (Lnode.peakLink != null)
Lnode.peakLink.nextLink = Lnode.nextLink;
if (Lnode.nextLink != null)
Lnode.nextLink.peakLink = Lnode.peakLink;
break;
}
temp++;
count--;
Lnode = Lnode.nextLink;
}
}
}
public void Print() //遍历所有节点
{
LinkedNode<T> Lnode = head;
while(Lnode != null )
{
Console.WriteLine("节点的值为:"+Lnode.vaule );
Lnode = Lnode.nextLink;
}
}
}
class Program
{
static void Main(string[] args)
{
Program text = new Program();
LinkedNode<int> node1 = new LinkedNode<int>(1);
LinkedNode<int> node2 = new LinkedNode<int>(2);
LinkedNode<int> node3 = new LinkedNode<int>(3);
Link<int> list = new Link<int>();
list.Add(node1);
list.Add(node2);
list.Add(node3);
Console.WriteLine("此时链表的长度为:" + list.Count);
list.RemoveAt(2);
list.Print();
}
}
LinkedList<int> list = new LinkedList<int>();
Random rand = new Random();
int temp = 10;
while(temp-->=1)
{
list.AddFirst(rand.Next(101));
}
//foreach遍历
//foreach (var item in list)
//{
// Console.WriteLine(item);
//}
LinkedListNode<int> node = list.First;
//节点遍历——头节点遍历
Console.WriteLine("从头部开始 遍历了");
while(node!= null )
{
Console.WriteLine(node.Value);
node = node.Next;
}
//节点遍历 ——尾节点遍历\
Console.WriteLine("从尾部开始 遍历了");
node = list.Last;
while (node != null)
{
Console.WriteLine(node.Value);
node = node.Previous ;
}
总结数组,list,Dectionary,Stack,Queue,LinkedList等存储容器。我们怎么来使用
⭐【Unityc#专题篇】之c#进阶篇】
⭐【Unityc#专题篇】之c#核心篇】
⭐【Unityc#专题篇】之c#基础篇】
⭐【Unity-c#专题篇】之c#入门篇】
⭐【Unityc#专题篇】—进阶章题单实践练习
⭐【Unityc#专题篇】—基础章题单实践练习
⭐【Unityc#专题篇】—核心章题单实践练习
你们的点赞 收藏⭐ 留言 关注✅是我持续创作,输出优质内容的最大动力!、