《C#高级编程》第9章:集合,ArrayList,Stack,Queue,SortedList

ArrayList

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyArrayList
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            Console.WriteLine(al.Capacity);
            Console.WriteLine(al.Count);


            al.Add("al1");
            al.Add("al2");
            al.Add("al3");

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            string[] str = new string[] { "str1","str2"};
            al.AddRange(str);

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            al.RemoveAt(1);

            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();

            al.RemoveRange(2, 2);
            foreach (string al_str in al)
            {
                Console.WriteLine(al_str);
            }
            Console.WriteLine();
          


        }
    }
}
 

//******************************************

Stack

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace MyStack
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack sk = new Stack();
            sk.Push("aaa");
            sk.Push("bbb");
            sk.Push("ccc");

            foreach (string item in sk)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("/nItem Pop():" + sk.Pop().ToString());
            Console.WriteLine("/nItem Peek():" + sk.Peek().ToString());
            Console.WriteLine("Second iteration: ");

            foreach (string item in sk)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine("");
            Console.WriteLine("******");
            Console.WriteLine("");


            // Creates and initializes the source Stack.
            Stack mySourceQ = new Stack();
            mySourceQ.Push("barn");
            mySourceQ.Push("the");
            mySourceQ.Push("in");
            mySourceQ.Push("cats");
            mySourceQ.Push("napping");
            mySourceQ.Push("three");

            // Creates and initializes the one-dimensional target Array.
            Array myTargetArray = Array.CreateInstance(typeof(String), 15);
            myTargetArray.SetValue("The", 0);
            myTargetArray.SetValue("quick", 1);
            myTargetArray.SetValue("brown", 2);
            myTargetArray.SetValue("fox", 3);
            myTargetArray.SetValue("jumped", 4);
            myTargetArray.SetValue("over", 5);
            myTargetArray.SetValue("the", 6);
            myTargetArray.SetValue("lazy", 7);
            myTargetArray.SetValue("dog", 8);

            // Displays the values of the target Array.
            Console.WriteLine("The target Array contains the following (before and after copying):");
            PrintValues(myTargetArray, ' ');

            // Copies the entire source Stack to the target Array, starting at index 6.
            mySourceQ.CopyTo(myTargetArray, 6);

            // Displays the values of the target Array.
            PrintValues(myTargetArray, ' ');

            // Copies the entire source Stack to a new standard array.
            Object[] myStandardArray = mySourceQ.ToArray();

            // Displays the values of the new standard array.
            Console.WriteLine("The new standard array contains the following:");
            PrintValues(myStandardArray, ' ');

        }


        public static void PrintValues(Array myArr, char mySeparator)
        {
            foreach (Object myObj in myArr)
            {
                Console.Write("{0}{1}", mySeparator, myObj);
            }
            Console.WriteLine();
        }
    }
      
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值