c# array arraylist 泛型list
1 array 数组 是存储相同类型元素的固定大小的数据的顺序集合。在内存中是连续存储的,所以索引速度非常快,而且赋值和修改元素也非常简单。
//定义字符串数组 大小为3
string[] str1 = new string[3];
str1[0] = "1";
str1[1] = "2";
str1[2] = "3";
//第二种赋值方式
string[] str2 = new string[] {"1","2","3" };
//第三种赋值方式
string[] str3 = { "1","2","3"};
2 arraylist对象的大小是按照其存储的数据来动态扩充的。声明arraylist对象不需要指定其长度。
//arraylist
ArrayList myal = new ArrayList();
myal.Add("HelloWorld");
myal.Add(12);
myal.Add(45.50);
arraylist可以存储不同类型的数据。装箱、拆箱特别麻烦。 arraylist会把所有插入的数据当做object对象来处理。
3 泛型 lsit<T> 数据类型是安全的,在声明list对象的时候,必须指定list集合内数据的对象类型.
List<string> myl = new List<string>();
myl.Add("123");
4 Dictionary Dictionary<string, string>是一个泛型
他本身有集合的功能有时候可以把它看成数组
他的结构是这样的:Dictionary<[key], [value]>
他的特点是存入对象是需要与[key]值一一对应的存入该泛型
通过某一个一定的[key]去找到对应的值
举个例子:
//实例化对象
Dictionary<int, string> dic = new Dictionary<int, string>();
//对象打点添加
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "one");
//提取元素的方法
string a = dic[1];
string b = dic[2];
string c = dic[3];
//1、2、3是键,分别对应“one”“two”“one”
//上面代码中分别把值赋给了a,b,c
//注意,键相当于找到对应值的唯一标识,所以不能重复
//但是值可以重复
list泛型的使用
ArrayList list = new ArrayList();
ArrayList list = new ArrayList(5); //可变数组
list.Add("我"); //Add 添加 向数组中赋值索引为最末尾
list.Add("今年");
list.Add("18");
list.Add("岁了");
list.Add("18");
list.Add("岁了");
list.Add("18");
list.Add("岁了");
知识点
list.Contains();//查询数组中元素是否存在
list.CopyTo(); //赋值数组中的全部数据
list.RemoveAt();//根据索引把指定位置元素删除
list.Remove(); //移除的是元素
list.Clear();//清空数组
list.Count; //数组元素的个数
举例子
bool result = list.Contains("我"); //返回bool 查询数组中元素是否存在
string[] list1 = new String[15];
list.CopyTo(list1); //赋值数组中的全部数据 赋值的集合的数据类型要一致
oblect[] list1 = new oblect[15];
list.CopyTo(list1); //赋值数组中的全部数据 赋值的集合的数据类型可以不一致
list.RemoveAt(6); //把第七个元素移除
list.Remove("我"); //移除的是元素
int index=list.Count;
Console.WriteLine(index);
Console.WriteLine(list[6]);
foreach (object val in list)
{
Console.WriteLine(val);
}
Console.ReadKey();
#endregion
#region 泛型集合
List<int> intList=new List<int>(); //List泛型集合 类型是一致的
intList.Add(12);
intList.Add(23);
intList.Add(34);
int result = intList.IndexOf(34);//根据元素,查找返回该元素的索引位置,如果没有,返回-1 可以指定索引范围(34,0,2)
//知识点
intList.Add();//查询数组中元素是否存在
intList.CopyTo(); //赋值数组中的全部数据
intList.RemoveAt();//根据索引把指定位置元素删除
intList.Remove(); //移除的是元素
intList.IndexOf(); //根据元素,查找返回该元素的索引位置,如果没有,返回-1
intList.Insert(1,16);//将元素插入集合某处
//举例子
intList[0] = 15;
intList.Insert(0,16);//将元素插入集合某处 原来索引位置的元素值自动下移,整理向后移动
Console.WriteLine(intList[0]);
Console.ReadKey();
#endregion
#region 泛型数组练习
List<string> arrList=new List<string>();
arrList.Add("张三");
arrList.Add("年龄");
arrList.Add("身高");
arrList.Add("电话号码");
int a = arrList.Count;
string name = "";
for (int i = 0; i < arrList.Count; i++)
{
if(arrList[0].Equals("张三"))
{
arrList[0] += "今年的";
Console.WriteLine(arrList[0]);
}
}
Console.ReadKey();
c# array arraylist 泛型list的更多相关文章
- 类 Array Arraylist List Hashtable Dictionary
总结C# 集合类 Array Arraylist List Hashtable Dictionary Stack Queue 我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashT ...
- c#中Array,ArrayList 与List<T>的区别、共性与转换
本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...
- Array,ArrayList,泛型List比较
在C#中数组Array,ArrayList,泛型List都能够存储一组对象,但是在开发中根本不知道用哪个性能最高,下面我们慢慢分析分析. 一.数组Array 数组是一个存储相同类型元素的固定大小的顺序 ...
- Array,ArrayList、List<T>、HashSet<T>、LinkedList<T>与Dictionary<K,V>
Array: 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. 但是数组存在一些不足的地方.在数组的两个数据间插入数据是很麻烦的,而且在声明数组的时候 ...
- [置顶] Array ArrayList LinkList的区别剖析
这是一个面试中我们经常被问到的问题 Array.ArrayList.LinkList之间的区别:Array.ArrayList.LinkList均属于泛型的范畴,都用来存放元素,主要区别是Array是 ...
- C# 集合类 Array,Arraylist,List,Hashtable,Dictionary...
我们用的比较多的非泛型集合类主要有 ArrayList类 和 HashTable类.我们经常用HashTable 来存储将要写入到数据库或者返回的信息,在这之间要不断的进行类型的转化,增加了系统装箱和 ...
- 解析C#中[],List,Array,ArrayList的区别及应用
[] 是针对特定类型.固定长度的. List 是针对特定类型.任意长度的. Array 是针对任意类型.固定长度的. ArrayList 是针对任意类型.任意长度的. Array 和 ArrayLis ...
- .net中的Array,ArrayList和List
Array:任意类型,定长 ArrayList:任意类型,不定长 List:特定类型,不定长 Array和ArrayList是通过存储object类型实现可以存储任意类型数据,使用时需要拆箱和装箱
- Array,ArrayList 和 List<T>的选择和性能比较.
Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...
随机推荐
- 数据绑定的知识点<%%>,<%#%>,<%=%>
1.<% %>用来绑定后台代码 如: < % for(int i=0;i<100;i++) { Reaponse.Write(i.ToString()); } %> 2. ...
- poj2104 K-th Number(划分树)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 66068 Accepted: 23273 Ca ...
- @property 的本质是什么?
将访问.变量.访问控制进行了绑定:编译器负责自动合成. @dynamic:不会自动合成成员变量和存取方法. @property 的本质是什么?@property = ivar + getter + s ...
- AlexNet (ImageNet模型)
介绍 AlexNet是LeNet的一种更深更宽的版本.首次在CNN中应用ReLU.Dropout和LRN,GPU进行运算加速. 一共有13层,有8个需要训练参数的层(不包括池化层和LRN层),前5层是 ...
- who---显示目前登录系统的用户信息
who命令是显示目前登录系统的用户信息.执行who命令可得知目前有那些用户登入系统,单独执行who命令会列出登入帐号,使用的终端机,登入时间以及从何处登入或正在使用哪个X显示器. 语法 who(选项) ...
- java解析json文件(省,市,区)
[{"code":"11","name":"北京市"},{"code":"12" ...
- 关于memset赋最值
出处[辗转山河弋流歌 by 空灰冰魂] blog.csdn.net/vmurder/article/details/46537613 memset(a, 0x3f, sizeof(a)) //int, ...
- 紫书 习题 10-4 UVa 1644(素数筛)
素数筛没什么好说的 #include<cstdio> #include<vector> #include<cstring> #define REP(i, a, b) ...
- 2014 CodingTrip - 携程编程大赛 (预赛第一场)
1001: 可以证明(扩展欧几里得),只要卡片中有两个卡片互素,旁边点就是可达的. 因此只需要算出所有卡片不互素的情况有多少种,可用容斥原理. #include <cstdio> #inc ...
- CodeForces 312B Archer
Archer Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ...