C#学习笔记之ArrayList和List(1)
2016-05-31 10:00
447 查看
数组只能存储一种类型,而数组列表能够存储不同类型的元素。但是取值的时候因为不知道类型,容易出现问题。
using System.Collections;//在c#中使用ArrayList必须引用Collections类;
一、优点
1. 支持自动改变大小的功能 2. 可以灵活的插入元素 3. 可以灵活的删除元素 4. 可以灵活访问元素
二、局限性
跟一般的数组比起来,速度上差些
现在我们通常使用List,但只能存储一个类型。以下是ArrayList和Array的各种使用方法:
using System;
using System.Collections.Generic;
using System.Collections;//在c#中使用ArrayList必须引用Collections类;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//数组列表
ArrayList al = new ArrayList();
al.Add(5);
al.Add(100);
al.Remove(5);
al.Add("wo le ge ca ");
foreach(var e in al){
Console.WriteLine(e);
}
Console.WriteLine(al[0]);
List<int> intList = new List<int>();
//添加一个元素
intList.Add(500);
//添加一串元素
intList.AddRange(new int[]{501,502});
intList.Insert(3,1001);
Console.WriteLine(intList.Contains(200));
Console.WriteLine(intList.IndexOf(501));
Console.ReadLine();
}
}
}
using System.Collections;//在c#中使用ArrayList必须引用Collections类;
一、优点
1. 支持自动改变大小的功能 2. 可以灵活的插入元素 3. 可以灵活的删除元素 4. 可以灵活访问元素
二、局限性
跟一般的数组比起来,速度上差些
现在我们通常使用List,但只能存储一个类型。以下是ArrayList和Array的各种使用方法:
using System;
using System.Collections.Generic;
using System.Collections;//在c#中使用ArrayList必须引用Collections类;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//数组列表
ArrayList al = new ArrayList();
al.Add(5);
al.Add(100);
al.Remove(5);
al.Add("wo le ge ca ");
foreach(var e in al){
Console.WriteLine(e);
}
Console.WriteLine(al[0]);
List<int> intList = new List<int>();
//添加一个元素
intList.Add(500);
//添加一串元素
intList.AddRange(new int[]{501,502});
intList.Insert(3,1001);
Console.WriteLine(intList.Contains(200));
Console.WriteLine(intList.IndexOf(501));
Console.ReadLine();
}
}
}
相关文章推荐
- C# Winform WebBrowser控件
- C#中TextBox水印提示的实现过程
- C# 中普通类、抽象类、接口之间的区别
- C#数字小写转大写
- C# 3.0新语言特性和改进(一)
- CSharpGL(25)一个用raycast实现体渲染VolumeRender的例子
- LINQ用法学习
- c#遍历一个文件夹下的所有文件包括子文件夹
- C#中问号的使用
- C#基础之内存分配
- C# 插入排序算法
- C#用链式方法
- C#中Math类的计算整数的三种方法
- C#通用类型转换方法
- C#用DataTable实现Group by数据统计
- c# MD5的加密和解密
- (C#基础) byte[] 之初始化, 赋值,转换。
- c#获取当前应用程序所在路径
- C# 字符串加密解密方法
- c# EF基础记录