C#数据结构

C#数据结构

常见结构

1、集合

2、线性结构

3、树形结构

4、图形结构

Array/ArrayList/List

特点:内存上连续存储,节约空间,可以索引访问,读取快,增删慢

using System;
namespace ArrayApplication
{
    class MyArray
    {
        
        static void Main(string[] args)
        {
            int[] list = { 34, 72, 13, 44, 25, 30, 10 };

            Console.Write("原始数组: ");
            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
           
            // 逆转数组
            Array.Reverse(list);
            Console.Write("逆转数组: ");
            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();
            
            // 排序数组
            Array.Sort(list);
            Console.Write("排序数组: ");
            foreach (int i in list)
            {
                Console.Write(i + " ");
            }
            Console.WriteLine();

           Console.ReadKey();
        }
    }
}

ArrayList

特点:元素没有类型限制,任何元素都是当成object处理,如果是值类型,会有装箱操作,不定长

ArrayList与数组的区别:数组容量固定,而ArrayList可以根据需要扩充;提供高效的添加删除等操作,相比数组效率不高;ArrayList提供只读和固定大小返回集合;ArrayList只能一维形式,数组可以是多维的;

构造器*3:

ArrayList List=new ArrayList ;	 //List:ArrayList对象名
for(i=0;i<10;i++)        //给ArrayList类对象添加10个int型元素
{
    List.Add(i);
}
int[]arr=new int[]{1,2,3,4,5,6,7,8,9};
ArrayList List=new(arr);
ArrayList List=new(10);
for(int i=0;i

1、Add

int arr[] =new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	//使用声明的数组实例化ArrayList对象
List.Add(7);			    //为ArrayList对象添加元素

2、Insert

int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new(arr);	//使用声明的数组实例化ArrayList对象
List.Insert(3,7);			//在ArrayList对象索引值=3处添加元素7

3、Clear

//使用 Clear()方法清除ArrayList中的所有元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	//使用声明的数组实例化ArrayList对象
List.Clear();			    //在ArrayList对象指定位置添加元素

4、Remove

//使用RemoveAt()方法从声明的ArrayList对象中移除与3匹配的元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new(arr);	//使用声明的数组实例化ArrayList对象
List.Remove(3);			    //删除ArrayList对象指定位置元素

5、RemoveRange

//在ArrayList对象中使用RemoveRange()方法从索引3处删除两个元素
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	   //使用声明的数组实例化ArrayList对象
List.RemoveRange(3,2);	       //删除ArrayList对象指定位置3处2个元素

6、Contains

//使用 Contains()方法判断数字2是否在ArrayList集合中
int[] arr = new int[]{1,2,3,4,5,6};
ArrayList List = new (arr);	   	    //使用声明的数组实例化ArrayList对象
Console.Write(List.Contains(2));	//ArrayList集合中是否包含指定的元素

7、IndexOf

public int IndexOf(String value);                  //语法1
public int IndexOf(char value);                    //语法2
public int IndexOf(String value, int startIndex);  //语法3
public int IndexOf(char value, int startIndex);    //语法4

8、LastIndexOf

public int LastIndexOf(String value);                   //语法1
public int LastIndexOf(char value);                     //语法2
public int LastIndexOf(String value, int startIndex);   //语法3
public int LastIndexOf(char value, int startIndex);     //语法4

9、foreach遍历

LinkedList

特点:非连续存储,存储数据和地址,只能顺序查找,读取慢,增删快,双向链表,泛型,保证类型安全,避免装箱拆箱,元素不连续分配,每个元素都记录前后节点,不定长

特性:

1)LinkedList 无法通过下标查找元素,在查找链表元素时,总是从头结点开始查找。

2)LinkedList 的容量是链表最大包含的元素数,会根据元素增减而动态调整容量

3)LinkedList 中的每个节点 都属于 LinkedListNode 类型

4)LinkedList 的值可以为 null,并允许重复值

5)LinkedList 不自带排序方法。

优点:不需要连续的内存空间,插入数据简单

缺点:每个节点离散,导致寻找越靠后效率越低

Dictionary

Dictionary dic = new Dictionary();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
foreach (var item in dic)
{
    Console.WriteLine($"Key:{item.Key} Value:{item.Value}");
}

SortedDictionary

SortedDictionary dic = new SortedDictionary();
dic.Add(1, "HaHa");
dic.Add(5, "HoHo");
dic.Add(3, "HeHe");
dic.Add(2, "HiHi");
dic.Add(4, "HuHu1");
dic[4] = "HuHu";                    
foreach (var item in dic)
{
    Console.WriteLine($"Key:{item.Key} Value:{item.Value}");
}

你可能感兴趣的:(c#,c#,数据结构,开发语言)