List,ArrayList

List

所属命名空间:System.Collections.Generic;

 List<T>类是  ArrayList 类的泛型等效类。 该类使用大小可按需动态增加的数
组实现  IList<T> 泛型接口。 如果对 IList<T> 类的类型 T 使用引用类型,则两个类的行为是完全相同
的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。   

用微软的话讲:“添加到 ArrayList  中的任何引用或值类型都将隐式地向上强制转换为 Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;

List的基础、常用方法:

  1、List<T> mList = new List<T>();    
T为列表中元素类型,现在以string类型作为例子
E.g.:  List<string> mList = new List<string>();
 
2、   List<T> testList =new List<T> (IEnumerable<T> collection);
      以一个集合作为参数创建List
       E.g.:
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
List<string> testList = new List<string>(temArr);
 
添加元素:   
1、  List. Add(T item)    添加一个元素
E.g.:     mList.Add("John");
2、   List. AddRange(IEnumerable<T> collection)   添加一组元素
E.g.:   
string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku",   "Locu" };
   
 
3、Insert(int index, T item);    在index位置添加一个元素
E.g.:     mList.Insert(1, "Hei");
遍历List中元素:   
foreach (T element in mList)  T的类型与mList声明时一样
            {
                 Console.WriteLine(element);
            } PS:如有问题请发送 E-mail 至:  xuzhanweics@163.com
E.g.:
foreach (string s in mList)
            {
                 Console.WriteLine(s);
            }
 
删除元素:
   1、  List. Remove(T item)       删除一个值
E.g.:    mList.Remove("Hunter");
    2、  List. RemoveAt(int index);   删除下标为index的元素
E.g.:     mList.RemoveAt(0);
                 3、  List. RemoveRange(int index, int count);   
从下标index开始,删除count个元素
       E.g.:    mList.RemoveRange(3, 2);
注:删除某元素后,其后面的元素下标自动跟进
 
判断某个元素是否在该List中:
List. Contains(T item)   返回true或false,很实用
E.g.:
if (mList.Contains("Hunter"))
            {
                 Console.WriteLine("There is Hunter in the list");
            }
             else
            {
                mList.Add("Hunter");
                 Console.WriteLine("Add Hunter successfully.");
            }
 
 
给List里面元素排序:List. Sort ()   默认是元素第一个字母按升序
E.g.:     mList.Sort();
给List里面元素顺序反转:
List. Reverse ()   可以与List. Sort ()配合使用,达到想要的效果
E.g.:     mList.Sort();
 
  List清空:List. Clear ()
E.g.:     mList.Clear();
  获得List中元素数目:
List. Count ()    返回int值
E.g.:
int count = mList.Count();
         Console.WriteLine("The num of elements in the list: " +count);
或者List.Count;
 
 
2、  List的进阶、强大方法:
举例用的 List:
string[] temArr = { Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", " "Locu" };
mList.AddRange(temArr);
 
 
List.Find 方法:搜索与指定谓词所定义的条件相匹配的元素,并返
回整个 List 中的第一个匹配元素。
public T Find(Predicate<T> match);
Predicate是对方法的委托,如果传递给它的对象与委托中定义的条件匹配,则该
方法返回 true。当前 List 的元素被逐个传递给Predicate委托,并在 List 中向前移动, 从第一个元素开始, 到最后一个元素结束。 当找到匹配项时处理即停止。  
Predicate  可以委托给一个函数或者一个拉姆达表达式
委托给拉姆达表达式:
E.g.:
              string listFind = mList.Find(name =>    //name是变量,代表的是mList
            {                               //中元素,自己设定
                 if (name.Length > 3)
                {
                     return true;
                }
                 return false;
            });
             Console.WriteLine(listFind);        //输出是Hunter 
 

ArrayList详解

System.Collections.ArrayList类是一个特殊的数组。通过添加和删除元素,就可以动态改变数组的长度。

一、优点

1. 支持自动改变大小的功能

2. 可以灵活的插入元素

3. 可以灵活的删除元素

4. 可以灵活访问元素

二、局限性

跟一般的数组比起来,速度上差些

三、添加元素

1.public virtual int Add(object value);

将对象添加到ArrayList的结尾处

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

内容为abcde

2.public virtual void Insert(int index,object value);

将元素插入ArrayList的指定索引处

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Insert(0,"aa");

结果为aaabcde

3.public virtual void InsertRange(int index,ICollectionc);

将集合中的某个元素插入ArrayList的指定索引处

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

ArrayList list2=new ArrayList();

list2.Add("tt");

list2.Add("ttt");

aList.InsertRange(2,list2);

结果为abtttttcde

四、删除

a)public virtual void Remove(object obj);

从ArrayList中移除特定对象的第一个匹配项,注意是第一个

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Remove("a");

结果为bcde

2.public virtual void RemoveAt(intindex);

移除ArrayList的指定索引处的元素

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveAt(0);

结果为bcde

3.public virtual void RemoveRange(int index,int count);

从ArrayList中移除一定范围的元素。Index表示索引,count表示从索引处开始的数目

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.RemoveRange(1,3);

结果为ae

4.public virtual void Clear();

从ArrayList中移除所有元素。

五、排序

a)public virtual void Sort();

对ArrayList或它的一部分中的元素进行排序。

ArrayListaList=newArrayList();

aList.Add("e");

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

DropDownList1.DataSource=aList;//DropDown ListDropDownList1;

DropDownList1.DataBind();

结果为eabcd

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Sort();//排序

DropDownList1.DataSource=aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

结果为abcde

b)public virtual void Reverse();

将ArrayList或它的一部分中元素的顺序反转。

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

aList.Reverse();//反转

DropDownList1.DataSource=aList;//DropDownListDropDownList1;

DropDownList1.DataBind();

结果为edcba

六、查找

a)public virtual int IndexOf(object);

b)public virtual int IndexOf(object,int);

c)public virtual int IndexOf(object,int,int);

返回ArrayList或它的一部分中某个值的第一个匹配项的从零开始的索引。没找到返回-1。

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("c");

aList.Add("d");

aList.Add("e");

intnIndex=aList.IndexOf(“a”);//1

nIndex=aList.IndexOf(“p”);//没找到,-1

d)public virtual int LastIndexOf(object);

e)public virtual int LastIndexOf(object,int);

f)public virtual int LastIndexOf(object,int,int);

返回ArrayList或它的一部分中某个值的最后一个匹配项的从零开始的索引。

ArrayList aList=new ArrayList();

aList.Add("a");

aList.Add("b");

aList.Add("a");//同0

aList.Add("d");

aList.Add("e");

intnIndex=aList.LastIndexOf("a");//值为2而不是0

g)public virtual bool Contains(objectitem);

确定某个元素是否在ArrayList中。包含返回true,否则返回false

七、获取数组中的元素

下面以整数为例,给出获取某个元素的值的方法

ArrayList aList=new ArrayList();

for(int i=0;i<10;i++)

     aList.Add(i);

for(i=0;i<10;i++)

    Textbox1.text+=(int)aList[i]+" ";//获取的方式基本与一般的数组相同,使用下标的方式进行

结果为:0 1 2 3 4 5 6 7 8 9

原文地址:https://www.cnblogs.com/wuxigang/p/2618061.html