新手菜鸟学习C#的笔记总结 之数组与集合(下)

本人是初学菜鸟,有错误的地方欢迎大家指正。

接上节……


集合的使用

1)动态数组类ArrayList,与固定数组类似,下面总结不同的地方

①元素添加

class Program
    {
        static void Main(string[] args)
        {
          ArrayList table=new ArrayList(10);//定义一个初始容量为10的动态数组
          table.Add(1);
        foreach(int t in table)
          Console.WriteLine(t);
        }
    }

②元素插入

 class Program
    {
        static void Main(string[] args)
        {
            ArrayList table=new ArrayList(20);          
            for (int i = 0; i < 10; i++)
                table.Add(i);
            table.Insert(2,1);//在索引2处插入数字1
            foreach(int t in table)
                Console.WriteLine(t);
        }
    }

③元素删除

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList table=new ArrayList(20);          
            for (int i = 0; i < 10; i++)
                table.Add(i);
            foreach (int t in table)
                Console.WriteLine(t);
            table.Clear();        //清除所有元素       
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList table=new ArrayList(20);          
            for (int i = 0; i < 10; i++)
                table.Add(10-i);
            table.RemoveAt(7);         //移除索引为7的元素
            table.Remove(7);            //移除第一个与 7 匹配的元素           
            foreach (int t in table)
                Console.WriteLine(t);    
        }
    }

④元素查找

class Program
    {
        static void Main(string[] args)
        {
            ArrayList table=new ArrayList(20);
            Boolean res = false;
            for (int i = 0; i < 10; i++)
                table.Add(10-i);
            res = table.Contains(7);    //查找集合中是否存在7这个元素
                Console.WriteLine(res);    
        }
    }

⑤元素容量调节

  static void Main(string[] args)
        {
            ArrayList table=new ArrayList(20);
            for (int i = 0; i < 10; i++)
                table.Add(10-i);
            Console.WriteLine(table.Capacity);
            table.TrimToSize();		//调节容量与现有元素个数一致
            Console.WriteLine(table.Capacity);
        }

⑥动态数组转换成静态数组

    class Program
    {
        static void Main(string[] args)
        {
            ArrayList table = new ArrayList(20);
            for (int i = 0; i < 10; i++)
                table.Add(10-i);
            int [] a=(int [])table.ToArray(typeof(int));//这种形式才行
            foreach(int t in a)
            Console.WriteLine(t);
        }
    }


2)队列Queue类(先进先出)
①队列的定义
class Program
    {
        static void Main(string[] args)
        {
            int[] table={1,2,3,4,5,6,7,8,9,10};
            Queue Line1 = new Queue();//默认初始化 容量32,增长系数2.0
            Queue Line2 = new Queue(10);//设置初始容量为10
            Queue Line3 = new Queue(10, 3);//设置初始容量为 10 ,增长系数3.0,增长3倍?
            Queue Line4 = new Queue(table);//以数组table来定义队列           
        }
    }

②元素添加与输出

class Program
    {
        static void Main(string[] args)
        {
            int[] table={1,2,3,4,5,6,7,8,9,10};
            Queue Line4 = new Queue(table);//以数组table来定义队列
            foreach (int t in Line4)
            {
                Console.WriteLine(t);
            }
            Line4.Enqueue(11);//将11加入队列末尾
            foreach (int t in Line4)
            {
                Console.WriteLine(t);
            }
            Console.WriteLine(Line4.Dequeue());//第一个元素 1 出列
        }
    }

3)堆栈类Stack类(先进后出)

①堆栈的定义

static void Main(string[] args)
        {
            int[] table = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Stack stack1 = new Stack();//默认容量为10
            Stack stack2 = new Stack(20);//初始化容量为20
            Stack stack3 = new Stack(table);//用数组table来初始化堆栈
        }
②入栈与出栈
 static void Main(string[] args)
        {
            int[] table = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            Stack stack3 = new Stack(table);//用数组table来初始化堆栈
            stack3.Push(11);//在末尾加入一个元素11
            Console.WriteLine(stack3.Pop());//后进先出,推出最后一个元素11
        }
4)哈希表Hashtable,键值与值对应

①Hashtable的定义

  class Program
    {
        static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            Hashtable hashtable2 = new Hashtable(20);//初始化容量20,感觉这容量似乎没用啊~?     
        }
    }
②Hashtable元素的添加(添加在头还是尾呢?)
 static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            hashtable1.Add(0, 10);//添加 键值为0 的数值 10
            hashtable1.Add(3, 8);//添加键值为3 的数值 8
            foreach (DictionaryEntry t in hashtable1)
            {
                Console.WriteLine("Key:{0}\t Value:{1}",t.Key,t.Value);
            }
        }
Hashtable元素的删除
 class Program
    {
        static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            Hashtable hashtable2 = new Hashtable(20);//初始化容量20,感觉这容量似乎没用啊~? 
            hashtable1.Add(0, 10);
            hashtable1.Add(1, 8);
            hashtable1.Add(2, 7);
            hashtable1.Clear();//删除所有元素
            foreach (DictionaryEntry t in hashtable1)
            {
                Console.WriteLine("Key:{0}\t Value:{1}",t.Key,t.Value);
            }
        }
    }
  class Program
    {
        static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            Hashtable hashtable2 = new Hashtable(20);//初始化容量20,感觉这容量似乎没用啊~? 
            hashtable1.Add(0, 10);
            hashtable1.Add(1, 8);
            hashtable1.Add(2, 7);
            hashtable1.Add(3, 6);
            hashtable1.Add(4, 5);
            hashtable1.Add(5, 4);
            hashtable1.Remove(3);   //删除键值为3的元素
            foreach (DictionaryEntry t in hashtable1)
            {
                Console.WriteLine("Key:{0}\t Value:{1}",t.Key,t.Value);
            }
        }
    }

④Hashtable元素的查找

    class Program
    {
        static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            Hashtable hashtable2 = new Hashtable(20);//初始化容量20,感觉这容量似乎没用啊~? 
            hashtable1.Add(10, 10);
            hashtable1.Add(9, 9);
            hashtable1.Add(8, 8);
            hashtable1.Add(7, 7);
            hashtable1.Add(6, 6);
            hashtable1.Add(5, 5);
            Console.WriteLine(hashtable1[10]);//读取键值为10 的元素的数值
            hashtable1[9] = 1;//修改键值为9 的元素的数组
            Console.WriteLine(hashtable1[9]);//输出结果
        }
    }

4)排序列表SortedList

①SortedList的定义

 class Program
    {
        static void Main(string[] args)
        {
            //简单的两种初始化
            Hashtable hashtable1 = new Hashtable();//使用默认初始化容
            hashtable1.Add(10, 10);
            hashtable1.Add(9, 9);
            hashtable1.Add(7, 7);
            hashtable1.Add(8, 8);
            hashtable1.Add(6, 6);
            hashtable1.Add(5, 5);
            SortedList sortlist1 = new SortedList();//默认容量定义
            SortedList sortlist2 = new SortedList(40);//定义初始容量为40
            SortedList sortlist3 = new SortedList(hashtable1);//以hashtable1初始化
        }
②SortedList元素的添加(无论以什么顺序添加,都会以键值顺序(从小到大)加入集合)

    class Program
    {
        static void Main(string[] args)
        {
           SortedList sortlist1 = new SortedList();//以hashtable1初始化
           sortlist1.Add(2, 2);
           sortlist1.Add(1, 1);
           sortlist1.Add(4, 4);
           sortlist1.Add(3, 3);
            foreach (DictionaryEntry t in sortlist1)
            {
                Console.WriteLine(t.Value);
            }            
        }
    }
SortedList 元素的删除

    class Program
    {
        static void Main(string[] args)
        {
           SortedList sortlist1 = new SortedList();//以hashtable1初始化
           sortlist1.Add(2, 2);
           sortlist1.Add(1, 1);
           sortlist1.Add(4, 4);
           sortlist1.Add(3, 3);
           sortlist1.Remove(1);//删除数值为1的数
           sortlist1.RemoveAt(2);//删除索引为2的元素
            foreach (DictionaryEntry t in sortlist1)
            {
                Console.WriteLine(t.Value);
            }
            sortlist1.Clear();//清楚所有元素
        }
    }
④SortedList元素的查找
   class Program
    {
        static void Main(string[] args)
        {
           SortedList sortlist1 = new SortedList();//以hashtable1初始化
           sortlist1.Add(2, 2);
           sortlist1.Add(1, 1);
           sortlist1.Add(4, 4);
           sortlist1.Add(3, 3);
           Console.WriteLine( sortlist1.IndexOfKey(4)); //找到键值为4的索引,结果为3
           Console.WriteLine(sortlist1.IndexOfValue(4));//找到数值为4的索引,结果为3
           Console.WriteLine("是否存在键值为3的数?:{0}",sortlist1.Contains(3));
           Console.WriteLine("是否存在键值为5的数?:{0}", sortlist1.ContainsKey(5));
           Console.WriteLine("是否存在数值为2的数?:{0}", sortlist1.ContainsValue(2));
        }
    }
⑤SortedList元素值的读取或修改(可以用使用下标)
class Program
    {
        static void Main(string[] args)
        {
           SortedList sortlist1 = new SortedList();//以hashtable1初始化
           sortlist1.Add(2, 2);
           sortlist1.Add(1, 1);
           sortlist1.Add(4, 4);
           sortlist1.Add(3, 3);
           sortlist1[1] = 10;
           Console.WriteLine(sortlist1[1]);//注意,此处的[1]不是索引为1,而是键值为1,结果是1
        }
    }
⑥SortedList,Hashtable,Stack,Queue集合复制到Array(数组)中  copyto()

  class Program
    {
        static void Main(string[] args)
        {
            int[] table1 = new int[20];
            int[] table2 = new int[20];
            int[] table3 = new int[20];
            int[] table4 = new int[20];

           Hashtable hashtable = new Hashtable();
           hashtable.Add(1, 1);hashtable.Add(2, 2);hashtable.Add(3, 3);hashtable.Add(4, 4);
           hashtable.Values.CopyTo(table1, 0);//hashtable->array

           SortedList sortlist = new SortedList(hashtable);          
           sortlist.Values.CopyTo(table2,0);//sortedlist->array
         
           Queue queue = new Queue(table2);
           queue.CopyTo(table3, 0);//queue->array

           Stack stack = new Stack(table3);
           stack.CopyTo(table4, 0);//stack->array

           foreach (int t in table1)
               Console.Write(t);
           Console.WriteLine();
           foreach (int t in table2)
               Console.Write(t);
           Console.WriteLine();
           foreach (int t in table3)
               Console.Write(t);
           Console.WriteLine();
           foreach (int t in table4)
               Console.Write(t);
           Console.WriteLine();
        }   
    }

输出结果:
43210000000000000000
12340000000000000000
12340000000000000000
00000000000000004321

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值