ArrayList  集合

ArrayList      

      集合:很多数据的一个集合       数组:长度不可变、类型单一

      集合的好处:长度可以任意改变  类型随便

     集合长度都的问题   很多数据的集合数组类型不可变 长度单一  

     ArrayList  list=new  ArrayList ; 它不是静态的,我们可以尝试的创建一个对象 
           //创建了一个集合对象
            ArrayList list = new ArrayList();
            //集合:很多数据的一个集合
            //数组:长度不可变、类型单一
            //集合的好处:长度可以任意改变  类型随便      

 1 namespace _02集合
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             ArrayList list = new ArrayList();
 8             list.Add(1);
 9             list.Add(3.14);
10             list.Add(true );//bool
11             list.Add("李白");//string
12             list.Add('');//char
13             list.Add(new int[] { 1, 2, 3, 4, 5, 6, 8, 9 });
14             list.AddRange(new int[] { 1, 2, 3, 4, 5 });//AddRange表示添加一个集合
15             list.Add(list );//自己放自己,没意义
16             for (int i = 0; i <list.Count; i++)//Count实际包含的元素个数
17             {
18                 if(list[i] is person )//如果它能转换成Person类型
19                 {
20                     ((person)list[i]).say();//强转的前提:必须装有子类对象 有继承关系
21                 }
22                 else if (list[i]is int[])
23                 {
24                     for (int j = 0; j<((int[])list[i]).Length ; j++)
25                     {
26                         Console.WriteLine(((int [])list[i])[j]);
27                     }
28                 }
29                 else
30                 {
31                     Console.WriteLine(list[i]);
32                 }
33              
34             }              
35         }

 ArrayLit集合长度的问题 

// 每次集合中实际包含的元素个数(count)超过了可包含元素的个数capcity
  //的时候集合就会向内存中申请多开启一倍的空间,来保证集合长度够用


 1  static void Main(string[] args)
 2          {
 3               ArrayList list = new ArrayList();
 4               list.Add(1);
 5               list.Add(1);
 6               list.Add(1);
 7               list.Add(1);
 8               list.Add(1);
 9               list.Add(1);
10              list.Add(1);      
11             Console.WriteLine(list .Count);//0
12              Console.WriteLine(list .Capacity);//0
13              //count 表示这个集合的元素实际包含的个数
14              //capcity 表示这个集合的元素可包含的个数
15              Console.ReadKey();
16 
17        }
18      }
19      class person
20     {
21          public void say()
22         {
23             Console.WriteLine("");
24          }
25     }
26  }

 


 

 

两个练习   创建一个集合,里面添加一些数字,求平均和和 最大值

 1     //创建一个集合,里面添加一些数字,求平均和和 最大值
 2             #region MyRegion       
 3             //ArrayList list = new ArrayList();
 4             //list.AddRange(new int[]{1,2,3,4,5});      
 5             //int sum=0;
 6             //int max=(int )list [0];
 7             //for (int i = 0; i < list .Count ; i++)
 8             //{
 9             //    if ((int)list [i]>max )
10             //    {
11             //        max =(int)list[i] ;
12             //    }
13             //    sum += (int)list[i];//转为int
14             //    //为什么能将一个object类型转为int类型
15             //    //因为你父类装的是子类对象,里氏转换
16             //}
17             //Console.WriteLine(sum);
18             //Console.WriteLine(sum/list.Count);
19             //Console.WriteLine(max);
20             //Console.ReadKey();
21             #endregion

 //写一个长度为10的集合,要求在里面随机地存放10个数字//但是要求所有的数字不重复     

 1   #region MyRegion      
 2             ArrayList list = new ArrayList();
 3             Random r = new Random();
 4             for (int i = 0; i < 10; i++)
 5             //为什么不能写list.Count  Count表示实际包含的个数(0)条件不成立
 6             //循环不执行
 7             {
 8                 int rNumber = r.Next(0, 10);
 9                 //集合中没有这个随机数
10                 if (!list.Contains(rNumber))//不包含
11                 {
12                     list.Add(rNumber);
13                 }
14                 else//集合中有这个随机数
15                 {
16                     //一旦产生了重复的随机数,这次循环就不算数
17                     i--;
18                 }
19             }
20             for (int i = 0; i < list.Count; i++)
21             {
22                 Console.WriteLine(list[i]);
23             }
24             Console.ReadKey();
25             #endregion

     //将一个数组中的奇数放到一个集合中 ,偶数放左边一个集合 最终放右边一个集合

 //将一个数组中的奇数放到一个集合中 ,偶数放左边一个集合 最终放右边一个集合
            #region MyRegion
            int[] number = { 1, 2, 3, 4, 5, 6, 7, 8 };
            //奇数集合
            List<int> lisji = new List<int>();
            //偶数集合
            List<int> lisou = new List<int>();
            for (int i = 0; i < number.Length; i++)
            {
                if (number[i] % 2 == 0)
                {
                    lisou.Add(number[i]);
                }
                else
                {
                    lisji.Add(number[i]);
                }
            }
            ////合并一个集合 在声明一个新集合
            //List<int> listsum = new List<int>();
            //listsum.AddRange(lisji);
            //listsum.AddRange(lisou);

            //直接让一个集合去添加另一个集合
            //lisou.AddRange(lisji);
            //foreach (var item in lisou  )
            //{
            //    Console.Write(item+" ");
            //}
            //Console.ReadKey();

            //谁在最前边就让谁去添加类型一个
            lisji.AddRange(lisou);
            foreach (var item in lisji)
            {
                Console.Write(item + " ");
            }
            Console.ReadKey();
            #endregion

 //提示用户输入一个字符串 ,通过foreach循环将用户输入的字符串赋值 给一个字符数组

 1             //提示用户输入一个字符串 ,通过foreach循环将用户输入的字符串赋值 给一个字符数组 
 2             #region MyRegion
 3             Console.WriteLine("请输入一个字符串");
 4             string input = Console.ReadLine();
 5             char[] chs = new char[input.Length];
 6             int i = 0;
 7             //把字符串给数组你首先要拿到字符串中每个元素
 8             foreach (var item in input)//collection要循环访问的数组和名称
 9             {
10                 chs[i] = item;//赋值给char类型的数组
11                 //item:是input每一个元素,
12                 i++;//每赋值一次i++
13             }
14             foreach (var item in chs)
15             {
16                 Console.Write(item);
17             }
18             Console.ReadKey(); 
19             #endregion

 

 

 

posted @ 2017-11-04 18:23  wb_enduo  阅读(285)  评论(0编辑  收藏  举报