集合与泛型>集合
集合可以分为泛型集合类和非泛型集合类。
泛型集合类一般位于System.Collections.Generic命名空间,非泛型集合类位于System.Collections命名空间,除此之外,System.Collection.Specialized命名空间也有些集合类。
数组集合类 System.Collections.ArrayList;
布尔集合类 System.Collections.BitArray;
队列 System.Collections.Queue;
堆栈 System.Collections.Stack;
哈希表 System.Collections.Hashtable;
排序集合类 System.Collections.SortedList;
集合与泛型>数组集合>创建列表
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ArrayListDemo1
{
class Program
{
static void Main( string [] args)
{
// 使用默认的初始容量创建ArrayList,该实例并没有任何元素。
ArrayList al1 = new ArrayList();
al1.Add( " 111 " );
al1.Add( " 222 " );
al1.Add( " 333 " );
DisplayResult(al1);
// 使用实现了ICollection接口的集合类来初始化新创建的ArrayList,该实例与参数中的集合具有相同的初始容量。
ArrayList al2 = new ArrayList(al1);
DisplayResult(al2);
// 经由指定一个整数值来初始化ArrayList的容量。
ArrayList al3 = new ArrayList( 20 );
DisplayResult(al3);
ArrayList al4 = ArrayList.Repeat( " ccc " , 4 );
DisplayResult(al4);
Console.ReadLine();
}
static void DisplayResult(ArrayList ls)
{
Console.WriteLine( "" );
if (ls.Count <= 0 )
{
Console.WriteLine( " 数合没有任何集合元素 " );
}
foreach ( object s in ls)
{
Console.Write(Convert.ToString(s));
}
}
}
}
集合与泛型>数组集合>添加元素
两种方法向ArrayList添加元素:
Add,将单个元素添加到列表的尾部。
AddRange,将实现ICollection接口的集合实例,添加到列表的尾部。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
// 声明一个接受20个元素的ArrayList
ArrayList al = new ArrayList( 20 );
// 使用ArrayList的Add方法添加集合元素。
al.Add( " 我是元素一 " );
al.Add( " 我是元素二 " );
al.Add( " 我是元素三 " );
al.Add( " 我是元素四 " );
string [] strs = { " 我是元素五 " , " 我是元素六 " , " 我是元素七 " , " 我是元素八 " };
// 使用AddRange方法添加实现了ICollections接口的集。并按集合参数中元素的顺序添加
al.AddRange(strs);
foreach ( string str in al)
{
Console.WriteLine(str);
}
Console.ReadLine();
}
}
}
集合与泛型>数组集合>插入元素
两种方法向ArrayList插入元素:
Add,将单个元素插入列表指定的索引位置。
AddRange,将实现ICollection接口的集合实例,插入到列表指定的索引位置。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
ArrayList al = new ArrayList( 20 );
// 使用Insert方法插入集合元素。
al.Insert( 0 , " 我是元素一 " );
al.Insert( 1 , " 我是元素二 " );
al.Insert( 2 , " 我是元素三 " );
al.Insert( 3 , " 我是元素四 " );
Queue qu = new Queue();
qu.Enqueue( " 我是元素五 " );
qu.Enqueue( " 我是元素六 " );
qu.Enqueue( " 我是元素七 " );
// 使用InsertRange方法插入集合元素。
al.InsertRange( 4 ,qu);
Console.WriteLine( " 使用Insert和InsertRange方法添加集合元素 " );
foreach ( string s in al)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
集合与泛型>数组集合>删除元素
有三种方法删除元素,
Remove,接受object类型的参数,删除指定元素值。
RemoveAt,接受int类型的参数,删除指定索引的元素。
RemoveRange,删除一定范围的元素。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
ArrayList al = new ArrayList( 20 );
al.AddRange( new string [ 8 ] { " 元素一 " , " 元素二 " , " 元素三 " , " 元素四 " , " 元素五 " , " 元素六 " , " 元素七 " , " 元素八 " });
al.Remove( " 元素二 " );
al.RemoveAt( 2 );
al.RemoveRange( 3 , 2 );
foreach ( string s in al)
{
Console.WriteLine(s);
}
Console.ReadLine();
;
}
}
}
集合与泛型>数组集合>排序>简单排序
使用Sort方法进行排序,集合中所有元素必须实现IComparable接口,否则,抛出异常。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
ArrayList al = new ArrayList();
al.AddRange( new string [ 8 ] { " Array1 " , " Array2 " , " Array3 " , " Array5 " , " Array4 " , " Array8 " , " Array7 " , " Array6 " });
al.Sort();
foreach ( string s in al)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}
集合与泛型>数组集合>排序>复杂排序
定义一个待排序类,自定义排序方法,开始排序处理。
using System.Collections.Generic;
using System.Text;
namespace AdvancedSort
{
/// <summary>
/// 定义书籍类。具有三种属性:书名,分类,价格。
/// </summary>
public class Book
{
private string _bookname;
public string BookName
{
get { return _bookname; }
set { _bookname = value; }
}
private string _bookcategory;
public string BookCategory
{
get { return _bookcategory; }
set { _bookcategory = value; }
}
public double _price;
public double Price
{
get { return _price; }
set { _price = value; }
}
public Book( string bookname, string bookcategory, double price)
{
_bookname = bookname;
_bookcategory = bookcategory;
_price = price;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace AdvancedSort
{
/// <summary>
/// 按价格从高到低排序
/// </summary>
public class InstanceCompare:IComparer
{
private const string _category = " C# " ;
#region IComparer<Book> 成员
public int Compare( object x, object y)
{
double i = ((Book)y).Price - ((Book)x).Price;
if (i > 0 )
return 1 ;
else if (i < 0 )
return - 1 ;
else
return 0 ;
}
#endregion
}
/// <summary>
/// 按照书名从高到低排序
/// </summary>
public class BookNameCompare:IComparer
{
#region IComparer 成员
public int Compare( object x, object y)
{
return new CaseInsensitiveComparer().Compare(((Book)y).BookName, ((Book)x).BookName);
}
#endregion
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace AdvancedSort
{
class Program
{
static void Main( string [] args)
{
Book[] books = new Book[ 8 ]{ new Book( " C#书籍z " , " C# " , 45.5 ), new Book( " C#书籍f " , " C# " , 55.8 ),
new Book( " Delphi书籍1 " , " Delphi " , 78 ), new Book( " C#书籍x " , " C# " , 55.9 ), new Book( " ASP.NET " , " ASP.NET " , 66 ),
new Book( " Delphi书籍2 " , " Delphi " , 79 ), new Book( " C#书籍y " , " C# " , 60 ), new Book( " C#书籍b " , " C# " , 80 )};
ArrayList al = new ArrayList(books);
Console.WriteLine( " 排序前的集合排列 " );
DispalyResult(al);
al.Sort( new InstanceCompare());
Console.WriteLine( " 按价格排序后的集合排列 " );
DispalyResult(al);
al.Sort( new BookNameCompare());
Console.WriteLine( " 按书名排序后的集合排列 " );
DispalyResult(al);
Console.ReadLine();
}
static void DispalyResult(ArrayList al)
{
foreach (Book book in al)
{
Console.WriteLine( " 书名:{0} 价格:{1} " ,book.BookName,book.Price);
}
}
}
}
集合与泛型>数组集合>排序>查找元素
有三种方法,自己msdn查,是IndexOf,LastIndexOf,BinarySearch.
BinarySearch是二分算法搜索。三个搜索方法,搜到的话,返回索引值,搜不到,返回-1.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
string [] str = { " 元素一 " , " 元素二 " , " 元素三 " , " 元素四 " , " 元素五 " , " 元素六 " };
ArrayList al = new ArrayList(str);
int i = al.IndexOf( " 元素三 " );
Console.WriteLine( " 元素三在集合中的位置是 " + i);
i = al.LastIndexOf( " 元素五 " );
Console.WriteLine( " 元素五在集合中的位置是 " + i);
int j = al.BinarySearch( " 元素三 " ); // al.BinarySearch("元素六")就找不到元素,很奇怪,不知道为什么
if (j > 0 )
Console.WriteLine( " 元素六在集合中的位置是 " + j);
else
Console.WriteLine( " 没有找到元素一 " );
Console.ReadLine();
}
}
}
集合与泛型>数组集合>排序>遍历列表
ArrayList内部有一个数组,可通过下标访问,而且 ArrayList实现了IEnumerable接口,所以遍历集合,可以使用for或foreach方法
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main( string [] args)
{
ArrayList al = new ArrayList( new string [ 6 ] { " 元素一 " , " 元素二 " , " 元素三 " , " 元素四 " , " 元素五 " , " 元素六 " });
// 使用for遍历
Console.WriteLine( " for " );
for ( int i = 0 ; i <= al.Count - 1 ; i ++ )
{
Console.Write(al[i]);
}
Console.WriteLine( "" );
Console.WriteLine( " foreach " );
// 使用foreach遍历
foreach ( object s in al)
{
Console.Write(s);
}
Console.ReadLine();
}
}
}