集合
ArrayList
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _0623ArrayList
{
class Program
{
static void Main(string[] args)
{
//數(shù)組:長(zhǎng)度不可變,類型單一
//集合:長(zhǎng)度任意改變,類型任意
ArrayList list1 = new ArrayList();
list1.Add('1');
list1.Add(12);
list1.Add(3.454);
list1.Add(new int[] { 1, 2, 3, 4, 5 });
Person p1 = new Person();
list1.Add(p1);
//添加集合用
list1.AddRange(new int[] { 12, 3, 24, 4, 5 });
//list1.Clear();清楚所有元素
//list1.RemoveAt(0) 根據(jù)索引刪除
//list1.RemoveAt(0,3) 根據(jù)索引移除一定范圍
//list1.Insert(1,"在位置1出入"); 位置1插入一個(gè)元素
//list1.InsertRange(1, new int[] { 232, 23, 23, 4 });在1的位置插入一個(gè)集合
for (int i = 0; i < list1.Count; i++)
{
//Console.WriteLine(list1[i]);
if (list1[i] is Person)
{
((Person)list1[i]).SayHello();
}
else if (list1[i] is int[])
{
}
else
{
Console.WriteLine(list1[i]);
}
}
}
public class Person
{
public void SayHello()
{
Console.WriteLine("I am a person");
}
}
}
}
- 集合長(zhǎng)度
count 表示集合中實(shí)際包含的元素個(gè)數(shù);
capacity 表示集合中可以包含的元素個(gè)數(shù);
Hashtable
- 無序,key不能重復(fù)
using System;
using System.Collections;
namespace _0623Hashtable
{
class Program
{
static void Main(string[] args)
{
Hashtable ht = new Hashtable();
ht.Add("name", "fxx");
ht.Add("gender", "female");
ht.Add("age", 18);
//ht.Add("age", 20); key必須唯一
//另外一種賦值,可以覆蓋原有的key
ht["id"] = 123;
Person p1 = new Person();
if (!ht.ContainsKey("abc"))
{
ht["abc"] = p1;
}
//取值
foreach (var item in ht.Keys)
{
if(ht[item] is Person)
{
((Person)ht[item]).SayHello();
}
else
{
Console.WriteLine("key is {0}, value is {1}", item, ht[item]);
}
}
}
public class Person
{
public void SayHello()
{
Console.WriteLine("I am a person.");
}
}
}
}
List范用類型
namespace _List泛型集合
{
class Program
{
static void Main(string[] args)
{
List<int> list1 = new List<int>();
{
list1.Add(1);
list1.Add(2);
list1.Add(3);
list1.Add(4);
list1.AddRange(new int[] { 100, 200, 300 });
//List泛型集合可以轉(zhuǎn)為數(shù)組
int[] nums = list1.ToArray();
//數(shù)組轉(zhuǎn)集合
char[] chs1 = new char[] { 'c', 'b', 'a' };
List<char> listchar = chs1.ToList();
};
}
}
}
裝箱,拆箱
裝箱:將值類型轉(zhuǎn)為引用類型
拆箱:將引用類型轉(zhuǎn)為值類型
代碼中要盡量避免裝箱,拆箱;
- 裝箱例子
namespace _zx_cx
{
class Program
{
static void Main(string[] args)
{
int a = 10;
object b = a;//裝箱,值給引用
int c = (int)b; //拆箱,引用給值
//所以值類型裝箱了10000次
ArrayList list1 = new ArrayList();//object類型
for (int i = 0; i < 10000; i++)
{
list1.Add(i);//i是Int類型,轉(zhuǎn)去引用,裝箱
}
}
}
}
- 未發(fā)生拆箱裝箱
namespace _zx_cx
{
class Program
{
static void Main(string[] args)
{
//這里沒有任何拆箱裝箱,看兩種類型是否發(fā)生拆箱裝箱,要看兩種類型是否存在繼承關(guān)系,有繼承關(guān)系才有可能發(fā)生
string str1 = "123123";//string 引用類型
int a = Convert.ToInt32(str1);//值類型
}
}
}
- 發(fā)生了裝箱
int n =10;
IComparable i =n;//有繼承關(guān)系,值到引用,所以發(fā)生了裝箱
Dictionary
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace _Dictionary
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dic1 = new Dictionary<int, string>();
dic1.Add(1, "fxx");
dic1.Add(2, "gjj");
/* foreach (var item in dic1.Keys)
{
Console.WriteLine("{0}-----{1}", item, dic1[item]);
}
*/
foreach (KeyValuePair<int,string>kv in dic1)
{
Console.WriteLine("{0}----{1}", kv.Key, kv.Value);
}
}
}
}
數(shù)組和集合的區(qū)別
- 數(shù)組
優(yōu)點(diǎn):由于他在內(nèi)存中是連續(xù)的,所以他的索引和查詢非常快
缺點(diǎn):長(zhǎng)度是固定的,之后處理麻煩
namespace test2
{
class JiangYou
{
static void Main(string[] args)
{
//ArrayList list1 = new ArrayList();
//指定長(zhǎng)度的數(shù)組
string[] str1 = new string[2];
str1[0] = "aaaa";
str1[1] = "bbbb";
str1[0] = "cccc";
//未指定長(zhǎng)度的數(shù)組
int[] num1 = { 1, 2, 3, 4 };
//num1[5] = 5;不能這么添加
Console.WriteLine(num1);
}
}
}
- ArrayList
優(yōu)點(diǎn):大小和長(zhǎng)度以及元素都是動(dòng)態(tài)的且不用指定類型
缺點(diǎn):雖然解決了數(shù)組的固定長(zhǎng)度問題,但是由于都是object類型,所以效率較低,造成類型不安全問題,帶來很大的能耗
namespace test2
{
class JiangYou
{
static void Main(string[] args)
{
int[] num1 = { 1, 2, 3, 4 };
Person p1 = new Person();
ArrayList list1 = new ArrayList();
list1.Add("23123");
list1.Add(num1);
list1.Add(p1);
Console.WriteLine(list1);
foreach (var item in list1)
{
Console.WriteLine(item);
}
}
public class Person
{
}
}
}
- List泛型
泛型List是ArrayList類的泛型等效類,大部分用法和ArrayList相同,在聲明時(shí),需要聲明List集合內(nèi)數(shù)據(jù)的對(duì)象類型
語(yǔ)法
List<T> ListOfT = new List<T>();
例
class Person
{
private string name; //姓名
private int age; //年齡
//創(chuàng)建Person對(duì)象
public Person(string name, int age)
{
this.name= name;
this.age = age;
}
}
//創(chuàng)建Person對(duì)象
Person p1 = new Person("張三", 23);
Person p2 = new Person("李四", 18);
Person p3 = new Person("王五", 41);
//創(chuàng)建類型為Person的對(duì)象集合
List<Person> persons = new List<Person>();
//將Person對(duì)象放入集合
persons.Add(p1);
persons.Add(p2);
persons.Add(p3);
//輸出第2個(gè)人的姓名
Console.Write(persons[1].name);
- 總結(jié)
- 數(shù)組的容量是固定的,我們只能一次獲取或設(shè)置一個(gè)元素的值,而ArrayList或泛型List的容量可根據(jù)需要自動(dòng)擴(kuò)充、修改、刪除或插入數(shù)據(jù)。
- 數(shù)組可以具有多個(gè)維度,而 ArrayList或泛型List始終只具有一個(gè)維度,但是我們可以輕松創(chuàng)建數(shù)組列表或列表的列表。
- 在決定使用泛型List還是使用ArrayList 類(兩者具有類似的功能)時(shí),記住泛型List類在大多數(shù)情況下執(zhí)行得更好并且類型安全。如果對(duì)泛型List類的類型使用object類型時(shí),則兩個(gè)類的行為是完全相同的。