1 數組型(Array)
1.1 Array ArrayList List
{
int [ ] intArray = new int [ 3 ] ;
intArray[ 0 ] = 123 ;
string [ ] strArray = new string [ ] { "123" , "456" } ;
}
{
ArrayList arrayList = new ArrayList ( ) ;
arrayList. Add ( "song" ) ;
arrayList. Add ( "fsd" ) ;
arrayList. Add ( 31 ) ;
arrayList[ 2 ] = 34 ;
arrayList. RemoveAt ( 0 ) ;
arrayList. Remove ( "fsd" ) ;
arrayList. Remove ( "s" ) ;
}
{
List< int > list = new List < int > ( ) { 1 , 2 , 3 , 4 } ;
list. Add ( 123 ) ;
list. Add ( 456 ) ;
list[ 6 ] = 1213 ;
}
2 鏈表型(Linked)
{
LinkedList< int > linkedList = new LinkedList < int > ( ) ;
linkedList. AddFirst ( 123 ) ;
linkedList. AddLast ( 456 ) ;
bool isContain = linkedList. Contains ( 123 ) ;
LinkedListNode< int > node = linkedList. Find ( 123 ) ;
linkedList. AddBefore ( node, 0 ) ;
linkedList. AddAfter ( node, 9 ) ;
linkedList. Remove ( 456 ) ;
linkedList. Remove ( node) ;
linkedList. RemoveFirst ( ) ;
linkedList. RemoveLast ( ) ;
linkedList. Clear ( ) ;
}
{
Queue< string > strQueue = new Queue < string > ( ) ;
strQueue. Enqueue ( "one" ) ;
strQueue. Enqueue ( "two" ) ;
strQueue. Enqueue ( "three" ) ;
strQueue. Enqueue ( "four" ) ;
strQueue. Enqueue ( "five" ) ;
foreach ( string num in strQueue)
{
Console. WriteLine ( num) ;
}
Console. WriteLine ( $"Dequeue:{strQueue.Dequeue()}" ) ;
Console. WriteLine ( $"Peek at next item to dequeue:{strQueue.Peek()}" ) ;
Console. WriteLine ( $"Dequeue:{strQueue.Dequeue()}" ) ;
Queue< string > copyQueue = new Queue < string > ( strQueue) ;
foreach ( var item in copyQueue)
{
Console. WriteLine ( item) ;
}
Console. WriteLine ( $"queueCopy.Contains():{copyQueue.Contains(" three")}" ) ;
Console. WriteLine ( $"copyQueue.Count = {copyQueue.Count}" ) ;
}
{
Stack< string > strStack = new Stack < string > ( ) ;
strStack. Push ( "one" ) ;
strStack. Push ( "two" ) ;
strStack. Push ( "three" ) ;
strStack. Push ( "four" ) ;
strStack. Push ( "five" ) ;
foreach ( string num in strStack)
{
Console. WriteLine ( num) ;
}
Console. WriteLine ( $"Pop:{strStack.Pop()}" ) ;
Console. WriteLine ( $"Pop at next item to dequeue:{strStack.Peek()}" ) ;
Console. WriteLine ( $"Pop:{strStack.Pop()}" ) ;
Stack< string > copyStack = new Stack < string > ( strStack) ;
foreach ( var item in copyStack)
{
Console. WriteLine ( item) ;
}
Console. WriteLine ( $"copyStack.Contains():{copyStack.Contains(" three")}" ) ;
Console. WriteLine ( $"copyStack.Count = {copyStack.Count}" ) ;
}
3 hash
{
HashSet< string > hashSet = new HashSet < string > ( ) ;
hashSet. Add ( "123" ) ;
hashSet. Add ( "two" ) ;
hashSet. Add ( "three" ) ;
hashSet. Add ( "four" ) ;
hashSet. Add ( "five" ) ;
foreach ( var item in hashSet)
{
Console. WriteLine ( item) ;
}
Console. WriteLine ( hashSet. Count) ;
Console. WriteLine ( hashSet. Contains ( "one" ) ) ;
{
HashSet< string > hashSet1 = new HashSet < string > ( ) ;
hashSet1. Add ( "123" ) ;
hashSet1. Add ( "1234" ) ;
hashSet1. Add ( "1235" ) ;
hashSet1. Add ( "1234" ) ;
hashSet1. Add ( "1236" ) ;
hashSet1. SymmetricExceptWith ( hashSet) ;
hashSet1. UnionWith ( hashSet) ;
hashSet1. ExceptWith ( hashSet) ;
hashSet1. IntersectWith ( hashSet) ;
hashSet1. ToList ( ) ;
hashSet1. Clear ( ) ;
}
}
{
SortedSet< string > sortedSet = new SortedSet < string > ( ) ;
sortedSet. Add ( "123" ) ;
sortedSet. Add ( "1234" ) ;
sortedSet. Add ( "1235" ) ;
sortedSet. Add ( "1234" ) ;
sortedSet. Add ( "1236" ) ;
foreach ( var item in sortedSet)
{
Console. WriteLine ( item) ;
}
Console. WriteLine ( sortedSet. Count) ;
Console. WriteLine ( sortedSet. Contains ( "123" ) ) ;
}
{
Hashtable hashTable = new Hashtable ( ) ;
hashTable. Add ( "123" , "456" ) ;
hashTable[ 234 ] = 4556 ;
hashTable[ 22 ] = 234 ;
hashTable[ 1 ] = 456 ;
hashTable[ 67 ] = 456 ;
foreach ( DictionaryEntry item in hashTable)
{
Console. WriteLine ( item. Key+ " = " + item. Value) ;
}
Hashtable hs = Hashtable. Synchronized ( hashTable) ;
}
{
Dictionary< int , string > dic = new Dictionary < int , string > ( ) ;
dic. Add ( 1 , "Hello" ) ;
dic. Add ( 2 , "World" ) ;
dic. Add ( 3 , "C#" ) ;
dic. Add ( 4 , ".NET" ) ;
dic. Add ( 5 , "Big" ) ;
dic[ 4 ] = "張三" ;
foreach ( var item in dic)
{
Console. WriteLine ( item. Key+ "=" + item. Value) ;
}
}
{
SortedDictionary< int , string > dic = new SortedDictionary < int , string > ( ) ;
dic. Add ( 1 , "Hello" ) ;
dic. Add ( 2 , "World" ) ;
dic. Add ( 3 , "C#" ) ;
dic. Add ( 4 , ".NET" ) ;
dic. Add ( 5 , "Big" ) ;
dic[ 4 ] = "張三" ;
foreach ( var item in dic)
{
Console. WriteLine ( item. Key + "=" + item. Value) ;
}
}
{
SortedList sortList = new SortedList ( ) ;
sortList. Add ( "First" , "Hello" ) ;
sortList. Add ( "Second" , "world" ) ;
sortList. Add ( "Thrid" , "1" ) ;
sortList[ "Thrid" ] = "---" ;
IList keyList = sortList. GetKeyList ( ) ;
IList valueList = sortList. GetValueList ( ) ;
sortList. TrimToSize ( ) ;
sortList. Remove ( "Second" ) ;
sortList. RemoveAt ( 0 ) ;
sortList. Clear ( ) ;
}
4 線程安全的集合
{
}