C#数组教程
第一篇:C#数组教程
IEnumerable 接口:公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
违规广告ection 接口扩展 IEnumerable;
[ComVisibleAttribute(true)]
public interface 违规广告ection : IEnumerable
{
//增加了公共属性:Count,IsSynchronized和SyncRoot
//增加了公共方法:CopyTo()
}
IDictionary 和 IList 则是扩展 违规广告ection 的更为专用的接口。
IDictionary 实现是键/值对的集合,如 Hashtable 类。
[ComVisibleAttribute(true)]
public interface IDictionary : 违规广告ection, IEnumerable
{
//公共属性: IsFixedSize, IsReadOnly, Item, Keys, Values
//公共方法:Add, Clear, Contains, GetEnumerator,Remove
}
某些集合(如 Queue 类和 Stack 类)限制对其元素的访问,它们直接实现 违规广告ection 接口。
如果 IDictionary 接口和 IList 接口都不能满足所需集合的要求,则从 违规广告ection 接口派生新集合类以提高灵活性。
例如:
// 假设MessageSet是变量集合。
IEnumerator enumerator = MessageSet.GetEnumerator();
string nextMessage;
enumerator.MoveNext();
while ( (nextMessage = enumerator.Current) != null)
{
DoSomething(nextMessage); // We only have read access
// to NextMessage
enumerator.MoveNext();
}
融会贯通:
using System;
using System.Collections;
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i
{
_people[i] = pArray[i];
}
}
public IEnumerator GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
}
}
III、探讨部分
C# []、Array、List、ArrayList 区别
[] 是针对特定类型、固定长度的。
Array 是针对任意类型、固定长度的。
List 是针对特定类型、任意长度的。
ArrayList 是针对任意类型、任意长度的。ArrayList 是数组的复杂版本。ArrayList 类提供在大多数 Collections 类中提供但不在 Array 类中提供的一些功能。
例如:
Array 的容量是固定的,而 ArrayList 的容量是根据需要自动扩展的。
如果更改了 ArrayList.Capacity 属性的值,则自动进行内存重新分配和元素复制。
ArrayList 提供添加、插入或移除某一范围元素的方法。
在 Array 中,您只能一次获取或设置一个元素的值。
使用 Synchronized 方法可以很容易地创建 ArrayList 的同步版本。
而 Array 将一直保持它直到用户实现同步为止。
ArrayList 提供将只读和固定大小包装返回到集合的方法。而 Array 不提供。
另一方面,Array 提供 ArrayList 所不具有的某些灵活性。例如:
可以设置 Array 的下限,但 ArrayList 的下限始终为零。
Array 可以具有多个维度,而 ArrayList 始终只是一维的。
特定类型(不包括 Object)的 Array 的性能比 ArrayList 好
这是因为 ArrayList 的元素属于 Object 类型,所以在存储或检索值类型时通常发生装箱和取消装箱。
要求一个数组的大多数情况也可以代之以使用 ArrayList。
它更易于使用,并且通常具有与 Object 类型的数组类似的性能。
Array 位于 System 命名空间中;ArrayList 位于 System.Collections 命名空间中。
其他数据集合:(以后课程介绍)
1、 Stack类,Queue类
2、 SortedList类
3、 Hashtable类
4、 泛型:位于System.Collections.Generic中,可以访问Stack, Queue, List, Dictionary, SortedDictionary等的泛型版本。
IV.思考题:
1、 扑克牌有54张,使用现有知识模拟一幅扑克牌的实例,尝试打乱扑克的顺序(类似洗牌过程)。[基础]
2、选择题:[基础]
int[,,] num1 = new int[2, a, b] {
{ { 1, 2, 3 },{ 4, 5, 6 } },
{ { 7, 8, 9 },{10,11,12} }
};
int[,,] num2 = new int[2, c, d] {
{ { 1, 2, 3 },{ 4, 5, 6 }, { 7, 8, 9 },{10,11,12}},
{ {13,14,15},{16,17,18},{19,20,21},{22,23,24} }
};
(1)其中:a,b,c,d的值分别应该是:
A. 2,3,3,4 B. 2,3,4,3 C.3,2,3,4 D.3,2,4,3
(2)num1.Length, num1.GetLength(1)及num2.Length,num2.GetLength(1)的值分别是:
A. 12,2,24,4 B. 12,3,24,3 C. 6,2,12,3 D. 3,2,3,3
3、 模拟上述扑克牌的洗牌及发牌过程(以斗地主为例)。[中高]
4、 如果有54张图片,你可以在第2题的基础上写出一个完整的斗地主游戏吗?[高级]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14601556/viewspace-557620/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/14601556/viewspace-557620/