ArrayList集合

使用前引用
using System.Collections;


ArrayList集合
实例化、初始化
ArrayList al = new ArrayList();

ArrayList与Array的名字很相似。
相同点:
(1)两者都实现了IList、ICollection、IEnumerable接口;
(2)两者都可以使用整数索引访问集合中的元素,包括读取和赋值,且集合中的索引都从0开始。
不同点:
(1)ArrayList是集合,而Array是数组;
(2)ArrayList是具体类,Array是抽象类;
(3)数组必须在实例化时指定元素的数量,该数量一旦确定就不可以更改了,而ArrayList扩展了这一点,当实例化一个ArrayList实例时可以不指定集合元素数(有默认初始容量),当然你也可以指定初始容量;
(4)获取数组的元素数时使用Length属性,而获取ArrayList集合的元素数时使用Count属性;
(5)数组可以有多维,而ArrayList只能是一维。

数组(Array)定义时需要固定长度
ArrayList 不用

al.Add() 添加值
al[0] 只能修改

ArrayList al = new ArrayList();
al.Add();
al[] = ;
Console.WriteLine(al[]);

al.Count ArrayList集合长度
al.Inster(,) 在某个位置插入值

ArrayList al = new ArrayList();
al.Add();
al.Add();
al.Insert(, );
Console.WriteLine(al[]);

al.Clear() 清空ArrayList集合
al.Clone() 克隆ArrayList集合
al.Contains() ArrayList 中是否包含某元素
al.IndexOf() 第一次出现该元素的索引号
al..LastIndexOf() 最后一次出现该元素的索引号
al.Remove() 移除第一次出现的该元素
al.RemoveAt() 移除指定所以位置的元素
al.Sort() 对ArrayList集合按照升序进行排列
al.Reverse() 翻转整个ArrayList集合

练习

System.Threading.Thread.Sleep(3000);//休眠
输入多个手机号码进入集合
进行3秒的随机滚动抽奖

                 ArrayList al = new ArrayList();
                 Random ra = new Random();
                 ;
                 for (; ; )
                 {
                     i++;
                     Console.WriteLine("输入\"输入完成\"结束");
                     Console.Write("请输入第" + i + "个手机号:");
                     string s = Console.ReadLine();
                     if (s.Contains("输入完成") || (s.Contains("输") && s.Contains("入") && s.Contains("完") && s.Contains("成")) || (s.Contains("完") && s.Contains("成")) || (s.Contains("结") && s.Contains("束")))
                     {
                         break;
                     }
                     else
                     {
                         al.Add(s);
                     }
                     Console.Clear();
                 }
                 Console.Write("输入完成,请按任意键抽奖...");
                 Console.ReadKey();
                 ; j < ; j++)
                 {
                     Console.Clear();
                     string str = (string)al[ra.Next(al.Count)];
                     Console.SetCursorPosition((Console.WindowWidth - str.Length * ) / , );
                     Console.WriteLine(str);
                     System.Threading.Thread.Sleep();
                 }
                 //作弊
                 //Console.Clear();
                 //Console.WriteLine("中奖啦就是你000010101010101011");

答案

输入姓名,语文,数学,英语

                Console.Write("请输入人数:");
                int renshu = int.Parse(Console.ReadLine());
                ArrayList names = new ArrayList();
                ;
                ;
                ; h < renshu * ; h += )
                {
                    Console.Write("请输入第" + ii + "个人的姓名:");
                    names.Add(Console.ReadLine());
                    Console.Write("请输入第" + ii + "个人的语文成绩:");
                    names.Add(double.Parse(Console.ReadLine()));
                    Console.Write("请输入第" + ii + "个人的数学成绩:");
                    names.Add(double.Parse(Console.ReadLine()));
                    Console.Write("请输入第" + ii + "个人的英语成绩:");
                    names.Add(double.Parse(Console.ReadLine()));
                    yinyuzong += (];
                    ii++;
                }
                foreach (object s in names)
                {
                    Console.Write(s + "\t");
                }
                Console.WriteLine("按照语文成绩排序");
                #region 语文
                ; i < renshu * ; i += )               //冒泡排序,成绩由大到小排好
                {
                    ; j < renshu * ; j += )
                    {
                        ] < (double)names[j])
                        {
                            string zhong = (string)names[i];
                            names[i] = names[j - ];
                            names[j - ] = zhong;

                            ];
                            names[i + ] = names[j];
                            names[j] = chengji;

                            chengji = (];
                            names[i + ] = names[j + ];
                            names[j + ] = chengji;

                            chengji = (];
                            names[i + ] = names[j + ];
                            names[j + ] = chengji;
                        }
                    }
                }
                foreach (object s in names)
                {
                    Console.Write(s + "\t");
                }

                Console.WriteLine();
                ; i < renshu * ; i += )
                {
                    Console.Write("姓名:" + names[i]);
                    Console.Write(]);
                    Console.Write(]);
                    Console.WriteLine(]);
                }
                #endregion

                Console.WriteLine(], names[], names[], names[]);
                Console.WriteLine("按照数学成绩排序");
                #region 数学
                ; i < renshu * ; i += )               //冒泡排序,成绩由大到小排好
                {
                    ; j < renshu * ; j += )
                    {
                        ] < (])
                        {
                            string zhong = (string)names[i];
                            names[i] = names[j - ];
                            names[j - ] = zhong;

                            ];
                            names[i + ] = names[j];
                            names[j] = chengji;

                            chengji = (];
                            names[i + ] = names[j + ];
                            names[j + ] = chengji;

                            chengji = (];
                            names[i + ] = names[j + ];
                            names[j + ] = chengji;
                        }
                    }
                }
                //foreach (object s in names)
                //{
                //    Console.Write(s + "\t");
                //}
                Console.WriteLine();
                ; i < renshu * ; i += )
                {
                    Console.Write("姓名:" + names[i]);
                    Console.Write(]);
                    Console.Write(]);
                    Console.WriteLine(]);
                }
                #endregion

                Console.WriteLine(], names[], names[], names[]);
                Console.WriteLine("英语平均分为:" + yinyuzong / renshu);

答案

特殊集合

Stack栈集合

//使用前引用
using System.Collections;

Stack栈集合
栈集合
先放进去的最后才能拿出来
实例化、初始化
Stack st = new Stack();

添加元素
st.Push(2);

弹出元素 弹出后不在存在于数组
只要使用就会从最后一个去除
st.Pop();

只取值不弹出
st.Peek();

遍历集合
foreach(int aa in st){ }

Queue队列集合

使用前引用
using System.Collections;

Queue队列集合
先放进去的先出

实例化、初始化
Queue que = new Queue();

添加元素
que.Enqueue(2);

弹出元素
从头弹出
que.Dequeue();

Hashtable哈希表集合

使用前引用
using System.Collections;

Hashtable哈希表集合
先进的后出,一个一个赋值,但只能一起取值

实例化、初始化
Hashtable hs = new Hashtable();

添加元素
hs.Add(1, "111");
hs.Add(2, "222");
hs.Add(3, "333");
hs.Add(4, "444");

单独取值 单取key  单取values
foreach(object aa in hs.Keys)
{
Console.WriteLine(aa);
}
foreach (object aa in hs.Values)
{
Console.WriteLine(aa);
}
同时取值
IDictionaryEnumerator ide = hs.GetEnumerator();
while(ide.MoveNext())
{
Console.WriteLine(ide.Key + "\t" + ide.Value);
}

2016年10月16日--ArrayList集合、特殊集合的更多相关文章

  1. 2016年10月16日 星期日 --出埃及记 Exodus 18:27

    2016年10月16日 星期日 --出埃及记 Exodus 18:27 Then Moses sent his father-in-law on his way, and Jethro returne ...

  2. 2016年10月31日 星期一 --出埃及记 Exodus 19:16

    2016年10月31日 星期一 --出埃及记 Exodus 19:16 On the morning of the third day there was thunder and lightning, ...

  3. 2016年12月16日 星期五 --出埃及记 Exodus 21:11

    2016年12月16日 星期五 --出埃及记 Exodus 21:11 If he does not provide her with these three things, she is to go ...

  4. 2016年11月16日 星期三 --出埃及记 Exodus 20:7

    2016年11月16日 星期三 --出埃及记 Exodus 20:7 "You shall not misuse the name of the LORD your God, for the ...

  5. 2016年10月30日 星期日 --出埃及记 Exodus 19:15

    2016年10月30日 星期日 --出埃及记 Exodus 19:15 Then he said to the people, "Prepare yourselves for the thi ...

  6. 2016年10月29日 星期六 --出埃及记 Exodus 19:14

    2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...

  7. 2016年10月28日 星期五 --出埃及记 Exodus 19:13

    2016年10月28日 星期五 --出埃及记 Exodus 19:13 He shall surely be stoned or shot with arrows; not a hand is to ...

  8. 2016年10月27日 星期四 --出埃及记 Exodus 19:12

    2016年10月27日 星期四 --出埃及记 Exodus 19:12 Put limits for the people around the mountain and tell them, `Be ...

  9. 2016年10月26日 星期三 --出埃及记 Exodus 19:10-11

    2016年10月26日 星期三 --出埃及记 Exodus 19:10-11 And the LORD said to Moses, "Go to the people and consec ...

随机推荐

  1. beautifulsoup测试

    import re from bs4 import BeautifulSoup html_doc = """ <html><head><ti ...

  2. 9月23日JavaScript作业----日期时间选择

    作业二:日期时间选择 <div style="width:600px; height:100px;"> <select id="year"&g ...

  3. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

  4. C#中导入Win32 API函数

    C#中导入Win32 API的方法: 1.引用命名空间 using System.Net.Security; using System.Runtime.InteropServices; 2. [Dll ...

  5. paramiko模拟ansible远程执行命令

    主模块 #!/usr/bin/env python from multiprocessing import Process import paramiko import time import sys ...

  6. Language Tool ,a plugin for TeXStudio

    Language Tool ,a plugin for TeXStudio TexStudio supports LanguageTool as an inline grammar checker. ...

  7. Quickling技术

    技术来自facebook,目的是实现ajax的seo友好. 简单来说就是骗爬虫,然后该写js还是写js. 原文在这里:http://www.cnblogs.com/haoming/p/3616326. ...

  8. c# 多线程 --Mutex(互斥锁)

    互斥锁(Mutex) 互斥锁是一个互斥的同步对象,意味着同一时间有且仅有一个线程可以获取它. 互斥锁可适用于一个共享资源每次只能被一个线程访问的情况 函数: //创建一个处于未获取状态的互斥锁 Pub ...

  9. 使用code标签获得类似代码段的效果

    几乎所有的浏览器都支持 code标签 code标签, 顾名思义,就是代码标签, imply tell browser, that 后面的部分是表示计算机代码. 因此, 浏览器可以根据自己的特点来显示这 ...

  10. JS抽奖功能代码

    HTML <label for="awardListDom">奖项列表</label><br> <input type="tex ...