C# 数组及集合
2023-09-11 14:16:46 时间
一 数组
声明:
int [] a;
int [,]b;
分配空间:
a=new int[5];
b=new int[4,5];
二 集合类
System.Collections命名空间;
数组列表ArrayList
相当于动态数组,实现IList;
哈希表 Hashtable
相当于键/值的集合,实现IDictionary;
用[] 进行访问,表示获取,增加、删除、修改;
提示:
用于查询时,比线性搜索的效率更高,可用于程序的优化;
栈和队列 Stack Queue;
三 使用foreach 访问数组及集合
foreach(类型 变量 in xxxx)
其中xxxx必须是实现了实现IEnumerable接口或含有GetEnumerator方法的类型;
这个方法的原型是IEnumerator GetEnumerator();
返回的是一个接口IEnumerator
Current属性
MoveNext及Reset方法;
四 泛型集合
泛型具有更好的类型检查及性能
System.Collections.Generic名称空间
列表 List/LinkedList SortedList
字典 Dictionary SortedDictionary;
集 HashSet SortedSet
栈,队列 Stack,Queue;
示例
1 ListTest
List 的Add方法,Count属性,[i]索引器;
foreach的遍历;
2 HashtableTest
HashTable的[] 索引,可以表示获取/加入/修改/删除(置为null)
Foreach遍历;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 使用List
{
public class ListTest
{
public static void Main()
{
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Carrot");
Console.WriteLine("Count:{0}", fruits.Count);
PrintValues1(fruits);
PrintValues2(fruits);
PrintValues3(fruits);
Console.ReadKey();
}
static void PrintValues1(IList<string>myList)
{
for (int i = 0; i < myList.Count; i++)
Console.Write("{0}\n", myList[i]);
}
static void PrintValues2(IList<string>myList)
{
foreach (string item in myList)
Console.Write("{0}\n", item);
}
static void PrintValues3(IEnumerable<string>myList)
{
IEnumerator<string> myEnumerator = myList.GetEnumerator();
while (myEnumerator.MoveNext())
Console.Write("{0}\n", myEnumerator.Current);
Console.WriteLine();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 使用Hashtable
{
internal class Class1
{
public static void Main()
{
Hashtable myHT = new Hashtable();
myHT.Add("Ton V.Bergyk", "023-010-66757");
myHT.Add("Tom sony", "086-010-27654");
myHT.Add("Mr.John", "071-222-33445");
myHT["mr.John"] = "071-222-33445";//加入或修改
PrintKeysAndValues(myHT);
PrintByKeys(myHT);
Console.ReadKey();
}
public static void PrintKeysAndValues(Hashtable myList)
{
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
while (myEnumerator.MoveNext())
Console.WriteLine("\\t{0}:t{1}",
myEnumerator.Key, myEnumerator.Value);
Console.WriteLine();
}
public static void PrintByKeys(Hashtable myList)
{
IEnumerator ie = myList.Keys.GetEnumerator();
while (ie.MoveNext())
{
object key = ie.Current;
object value = myList[key];
Console.WriteLine("\t{0}:\t{1}", key, value);
}
Console.WriteLine();
foreach (string key in myList.Keys)
{
Console.WriteLine("\t{0}:\t{1}", key, myList[key]);
}
}
}
}
相关文章
- C# 版dll 程序集合并工具
- C#将list集合转化为sql
- C# 并发安全集合ConcurrentBag取代List
- C#使用集合组织相关数据
- C#集合--Dictionary
- 重学c#系列——异常续[异常注意事项](七)
- .NET/C#中对自定义对象集合进行自定义排序的方法
- [c#基础]集合foreach的必要条件和自定义集合
- mysql 在c# EF 中无法生成对象
- c# checked 和 unchecked
- C# 容器上控件排序
- C#中使用Redis不同数据结构的内存占有量的疑问和对比测试
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
- Atitit.得到网络邻居列表java php c#.net python
- 编程笔试(解析及代码实现):猴子吃桃。猴子第一天吃了若干个桃子,当即吃了一半,还不解馋,又多吃了一个…的C++、Java、Python、C#等语言代码实现
- 《[C#] int与System.Int32有什么区别》
- (三十二)c#Winform自定义控件-表格-HZHControls
- CSharp(C#)语言_高级篇(泛型集合)
- C#集合类:动态数组、队列、栈、哈希表、字典(转)
- C# 并发安全集合ConcurrentBag取代List
- C++CLR类库封装Native类库并用C#调用 - 草稿
- [C#]c#中数据的同步加锁机制 的几种方法
- c#数组和集合