c#list基礎用法彙總
因為是轉載文章, 在此標明出處,如有冒犯請聯絡本人。
因為好的文章,以前只想收藏,但連線有時候會失效,所以現在碰到好的直接轉到自己這裡。
原文出處:http://www.33lc.com/article/7364.html
C#中的List怎麼樣?List<T>類是ArrayList類的泛型等效類,該類使用大小可按需動態增加的陣列實現IList<T>泛型介面。接下來,綠茶小編就介紹一些List的基礎簡單用法。
泛型的好處:它為使用c#語言編寫面向物件程式增加了極大的效力和靈活性。不會強行對值型別進行裝箱和拆箱,或對引用型別進行向下強制型別轉換,所以效能得到提高。
效能注意事項:在決定使用IList<T>還是使用ArrayList類(兩者具有類似的功能)時,記住IList<T>類在大多數情況下執行得更好並且是型別安全的。如果對IList<T>類的型別T 使用引用型別,則兩個類的行為是完全相同的。但是,如果對型別T 使用值型別,則需要考慮實現和裝箱問題。
C# List的基礎常用方法:
一、宣告:
1、List<T> mList = new List<T>();
T為列表中元素型別,現在以string型別作為例子:
List<string> mList = new List<string>();
2、List<T> testList =new List<T> (IEnumerable<T> collection);
以一個集合作為引數建立List:
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"};
List<string> testList = new List<string>(temArr);
二、新增元素:
1、List. Add(T item)新增一個元素
例:
mList.Add("John");
2、List. AddRange(IEnumerable<T> collection)新增一組元素
例:
string[] temArr = {"Ha","Hunter","Tom","Lily","Jay","Jim","Kuku","Locu"};mList.AddRange(temArr);
3、Insert(intindex, T item);在index位置新增一個元素
例:
mList.Insert(1,"Hei");
三、遍歷List中元素:
foreach(TelementinmList)T的型別與mList宣告時一樣
{
Console.WriteLine(element);
}
例:
foreach(stringsinmList)
{
Console.WriteLine(s);
}
四、刪除元素:
1、List. Remove(T item)刪除一個值
例:
mList.Remove("Hunter");
2、List. RemoveAt(intindex);刪除下標為index的元素
例:
mList.RemoveAt(0);
3、List. RemoveRange(intindex,intcount);
從下標index開始,刪除count個元素
例:
mList.RemoveRange(3, 2);
五、判斷某個元素是否在該List中:
List. Contains(T item)返回true或false,很實用
例:
if(mList.Contains("Hunter"))
{
Console.WriteLine("There is Hunter in the list");
}
else
{
mList.Add("Hunter");
Console.WriteLine("Add Hunter successfully.");
}
六、給List裡面元素排序:
List. Sort ()預設是元素第一個字母按升序
例:
mList.Sort();
七、給List裡面元素順序反轉:
List. Reverse ()可以不List. Sort ()配合使用,達到想要的效果
例:
mList.Sort();
八、List清空:
List. Clear ()
例:
mList.Clear();
九、獲得List中元素數目:
List. Count ()返回int值
例:
in tcount = mList.Count();
Console.WriteLine("The num of elements in the list: "+count);
相關推薦
c#list基礎用法彙總
因為是轉載文章, 在此標明出處,如有冒犯請聯絡本人。 因為好的文章,以前只想收藏,但連線有時候會失效,所以現在碰到好的直接轉到自己這裡。 原文出處:http://www.33lc.com/article/7364.html C#中的List怎麼樣
C++ List的用法(整理)
Lists將元素按順序儲存在連結串列中. 與 向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢. assign() 給list賦值 back() 返回最後一個元素 begin() 返回指向第一個元素的迭代器 clear() 刪除所有
python中 list的用法彙總
list 列表 用 [] “中括號”表示 List = [1,1,2,3,5,8,9,4,1,6,1,1,1,'L'] List >>> [1, 1, 2, 3, 5, 8, 9, 4, 1, 6, 1, 1, 1, 'L'] 1、count()查詢元素
C++ list 的用法
{ //list1物件初始為空 INTLIST list1; //list2物件最初有10個值為6的元素 INTLIST list2(10,6); //list3物件最初有3個值為6的元素 INTLIST list3(list2.begin(),--list2.
基礎知識之 - C# Using的用法
spa 方案 image acl 決定 ogr 托管 sid 引入 C#裏面Using有兩種用法: 1.作為指令。 using+命名空間,導入其他命名空間中定義的類型,這樣可以在程序中直接用命名空間中的類型,不必指定命名空間; 命名空間是.NET程序在邏輯上
C# list用法
強制 name itl 對象 檢查 als range cat foreach C# list介紹 一、LIST概述 所屬命名空間:System.Collections.Generic public class List<T> : IList&
【轉】C# list介紹和用法
php 檢索 排序 c# reac ont 面向對象 類型 大型 一、LIST概述 所屬命名空間:System.Collections.Generic public class List<T> : IList<T>, ICollection
C# List<T>用法詳解
知新樹 寧金峰 所屬命名空間:System.Collections.Generic public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnu
python基礎(list基本用法)
多維數組 iphone 裏的 tin abc pass list排序 查找 mov List基本用法: #list 是用“[]”定義 用“,”隔開 例子: #stu_name=‘海龍‘,‘楊帆‘,‘劉榮心‘,‘......‘ #定義一個名為stu_name的list
C# List的詳細用法
作者:沐雪 連結: http://www.cnblogs.com/puzi0315/archive/2012/08/08/2628981.html C# List<T>的詳細用法 所屬名稱空間:System.Coll
C語言易錯基礎知識彙總
以下整理自湖南大學考研真題。 2010年 1、C語言源程式檔案經過C編譯程式編譯連線之後生產一個字尾為“.exe”的檔案 2、以下選項中不正確的整型常量是() A、12L B、-10 C、1,900 D、123U 3、表示式_____不
C++基礎筆記——彙總版(上)
前言: 部落格開始記錄了一系列C++筆記,但各章節太分散,且編輯格式不是很規範,不便於檢視;現把各章節做一個彙總,準備放在一起,方便按知識點檢視。彙總版的C++基礎筆記包括上、下兩部分。 程式的組成部分 C++是面向物件程式設計,有如下特點。1.封裝、2
c++ list, vector, map, set 區別與用法
List封裝了連結串列,Vector封裝了陣列, list和vector得最主要的區別在於vector使用連續記憶體儲存的,他支援[]運算子,而list是以連結串列形式實現的,不支援[]。 Vector對於隨機訪問的速度很快,但是對於插入尤其是在頭部插入元素速度很慢,在
c++ list, vector, map, set 區別與用法比較
List封裝了連結串列,Vector封裝了陣列, list和vector得最主要的區別在於vector使用連續記憶體儲存的,他支援[]運算子,而list是以連結串列形式實現的,不支援[]。 Vector對於隨機訪問的速度很快,但是對於插入尤其是在頭部插入元素
C ++ list 用法整理(官網例子)
因為最近一直在用 C++ 的 list,搜到的那個最高訪問的 CSDN 博文寫的不夠簡潔。而官網上的一些例子有些細節沒涉及到,得自己再複製下來跑一遍才能搞明白。於是便萌生了寫這篇博文的念頭,方便自己以後查閱。 本文的例子都來自官網的介紹:http://www.cplusplus.co
C++中List的用法
Lists將元素按順序儲存在連結串列中. 與 向量(vectors)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢. assign() 給list賦值 back() 返回最後一個元素 begin() 返回指向第一個元素的迭代器 clear() 刪除所有元素
C語言基礎 遞迴的用法及示例
---------------start reading---------------- 前言 遞迴在數學證明中經常使用,其意就是不停的套用同一模板逐漸歸納出最後的結果,用通俗的話來說就是自己呼叫自己。 遞迴呼叫需要注意三個問題 縮小問題規模 邊界條件 返回段
c++中sort基礎用法
用法一:陣列排序 對一個數組進行升序排序 #include <algorithm> #include <iostream> #include <cstdio> using namespace std; int m
C++基礎知識彙總
面試C++程式設計師的時候一般都是3板斧,先是基礎問答,然後一頓虛擬函式、虛擬函式表、純虛擬函式、抽象類、虛擬函式和解構函式、虛擬函式和建構函式。接著拷貝建構函式、操作符過載、下面是STL,最後是智慧指標。 都能挺過去那基本知識這關應該算是
C# Dictionary用法彙總(定義、遍歷、排序)
1、Dictionary定義 Dictionary<string, double> dic = new Dictionary<string, double>(); dic.Add("語文", 98.5); dic["數學"] = 9