The collection class Array List HashTable instance operation exercise

  • 2020-05-27 04:42:57
  • OfStack

Common operations to add, traverse, and remove collections
The namespace System.Collections

ArrayList variable length array, which is used like an array
Attribute Capacity Count
methods
Add() AddRange() Remove() RemoveAt() Clear()
Contains() ToArray()
A collection of Hashtable key-value pairs (KeyValuePair), similar to a dictionary

a, ArrayList operations on value types
 
using System; 
using System.Collections; 
namespace _08_ArrayList { 
//ArayList Operations on value types  
class Program { 
static void Main( string[] args) { 
//ArrayList It's not that different from an array   The advantage is that unlike arrays, you don't have to specify the length   The disadvantage is that the data type is unrestricted   What kind of data can you put in   This can lead to a lot of errors  
ArrayList arylist = new ArrayList(); 
//ArrayList add  
arylist.Add(1000); 
//arylist.Add(" zhang 3");// The parameter type is object  So you can add multiple types of parameters   Type conversion is also required when fetching  
arylist.Add(3000); 
arylist.Add(4000); // Boxed operation occurs   Converts a value type to a reference type  
arylist.Add(5000); 
int [] arr = { 1, 2, 3, 4 }; 
arylist.AddRange(arr); //AddRange The parameter is implemented ICollections Object of interface   can 1 Sub-add array, array , ArrayList And the object that implements the interface  
// The number of elements in the set   use Count =  An array of Length 
Console .WriteLine(" Set content length " + arylist.Count); 
//Capacity Is the capacity of the collection   Is variable  1 a *2 growth  
Console .WriteLine(arylist.Capacity); 
// Access set control 1 An element  
int firstlist = Convert .ToInt32(arylist[0]); 
Console .WriteLine(firstlist.ToString()); 
//ArrayList traverse  
int sum2 = 0; 
for (int i = 0; i < arylist.Count; i++) { 
//sum2 += Convert.ToInt32(arylist[i]);// Unpacking occurs  
Console .WriteLine(arylist[i].ToString()); 
} 
foreach (object item in arylist) { 
sum2 += Convert .ToInt32(item); 
} 
Console .WriteLine(sum2); 
//ArrayList remove   Just remove the   Not delete  
arylist.Remove(1000); // To remove the content is 1000 the  Remove Removes an internal object  
arylist.RemoveAt(1); // Remove the first 2 item   Remove by index  
// Pay attention to   Remove elements  ArrayList The array will reassign the index   So the best way to remove an element is to go backwards  
// If I remove all the elements   Direct use of Clear 
//arylist.Clear(); 
if (arylist.Contains(3000)) { 
Console .WriteLine(" contains " ); 
} 
//ArrayList There are ToArray() But it doesn't make much sense  
// This is in ArrayList Add a value type to   What about reference types ???? add Student Class object?  
Console .Read(); 
} 
} 
} 

Operations on reference types by b and ArrayList
 
using System; 
using System.Collections; 
namespace _09_ArrayListObject { 
//ArrayList Operations on reference types  
class Student { 
public Student(string name, int age) { 
this .name = name; 
this .age = age; 
} 
private string name; 
public string Name { 
get { 
return name; 
} 
set { 
name = value ; 
} 
} 
private int age; 
public int Age { 
get { 
return age; 
} 
set { 
age = value ; 
} 
} 
} 
class Program { 
static void Main( string[] args) { 
Student xyy = new Student( " Xiao yueyue " , 14); 
Student fj = new Student( " feng " , 18); 
Student fr = new Student( " Sister lotus " , 19); 
Student xl = new Student( " Brother sharp " , 20); 
ArrayList student = new ArrayList(); 
student.Add(xyy); // add   You can also use AddRange 
student.Add(fj); 
student.Add(fr); 
student.Add(xl); 
// remove  
//student.Remove(fj);// So that's the object that you're removing   Not a value  
//student.RemoveAt(1);// Index to remove  
// Remove not to drop fj  because Remove After is object  Remove by index  
//Student stu = new Student(" feng ", 18); 
//student.Remove(stu); 
//Console.WriteLine(student.Contains(stu));//false  Indexed retrieval   because stu with fj The address is not 1 The sample of  
// traverse  
for (int i = 0; i < student.Count; i++) { 
Student s = student[i] as Student; // Because boxing occurs before adding   so   We need to unpack it now  student[i] I can't point it out name the  
Console .WriteLine(s.Name); 
} 
ArrayList ary = new ArrayList(); 
ary.Add( " feng " ); 
ary.Add( " Xiao yueyue " ); 
//string A class is also a reference type   But there's something special here  
string name = " feng " ; 
Console .WriteLine(ary.Contains(name));//string The comparison is content   So back true 
// Student names   Get student objects   although ArrayList Can be implemented   But it's pretty complicated   And it's inefficient   So let's move on HashTable 
Console .Read(); 
} 
} 
} 

c, HashTable
 
using System; 
using System.Collections; 
namespace _10_HashTable { 
class Student { 
public Student(string name, int age) { 
this .name = name; 
this .age = age; 
} 
private string name; 
public string Name { 
get { 
return name; 
} 
set { 
name = value ; 
} 
} 
private int age; 
public int Age { 
get { 
return age; 
} 
set { 
age = value ; 
} 
} 
} 
class Program { 
static void Main( string[] args) { 
// Still use Student Class to implement  
//Hashtable Key value pair form  key value  Equivalent to a dictionary   Can quickly find the object according to the student's name  
Student xyy = new Student( " Xiao yueyue " , 14); 
Student fj = new Student( " feng " , 18); 
Student fr = new Student( " Sister lotus " , 19); 
Student xl = new Student( " Brother sharp " , 20); 
Hashtable student = new Hashtable(); 
student.Add( " Xiao yueyue " , xyy); 
student.Add( " feng " , fj); 
student.Add( " Sister lotus " , fr); 
student.Add( " Brother sharp " , xl); 
//student.Add(" Brother sharp ",xl);// error   A key word in a dictionary key  No repetition   So you can't add brother sharp  
// remove   Because there is no index   So there is no RemoveAt() 
student.Remove( " Xiao yueyue " );// According to the key To remove  
student.Clear(); 
student.ContainsKey( " feng " );// To see if it contains this bond  
// traverse   Because the dictionary has no index   So you can't use it for To traverse   Can only use foreach 
// According to the key traverse   Often use  
foreach (object key in student.Keys) { 
Student stu = student[key] as Student; 
Console .WriteLine(key); 
Console .WriteLine(stu.Age); 
} 
// According to the value traverse  
foreach (object value in student.Values) { 
Student stu = value as Student; 
if (stu != null ) { 
Console .WriteLine(stu.Age); 
} 
} 
// If you don't press the key  Don't press the value traverse   To traverse the dictionary is to traverse the dictionary's key-value pairs  
foreach (DictionaryEntry de in student) { 
Console .WriteLine(de.Key); 
Student s = de.Value as Student; // Because what you get is object type   so   You also need a transformation to make it usable  
Console .WriteLine(s.Age); 
} 
Student s2 = student[" Xiao yueyue " ] as Student ;// Find the object by name   Get the other properties  
if (s2 != null ) { 
Console .WriteLine(s2.Age); 
} 
Console .Read(); 
} 
} 
} 

d, practice
 
using System; 
using System.Collections; 
namespace _11_ArrayList practice  { 
class Program { 
// again   After understanding the problem   Start writing when you have an idea code  Thinking is the most important thing  
static void Main( string[] args) { 
// Two sets {  " a " , " b " , " c " , " d " , " e " } and {  " d " ,  " e " ,  " f " ,  " g " ,  " h "  } , merge the two sets of removal duplicates 1 a  
ArrayList ary1 = new ArrayList { "a" , "b" , "c", "d" , "e" }; 
ArrayList ary2 = new ArrayList { "d" , "e" , "f", "g" , "h" }; 
// You go through two sets  
for (int i = 0; i < ary2.Count; i++) { // To iterate over ary2 Elements and ary1 Then compare   If you have the same value   Do not add   Otherwise append to ary1 In the  
if (!ary1.Contains(ary2[i])) {// There are Contains methods   If there is no   I don't know how complicated it is  
ary1.Add(ary2[i]); 
} 
} 
foreach (object item in ary1) { 
Console .Write(item); 
} 
// Randomly generated 10 a 1-100 The number between ArrayList So, we want to do that 10 You can't repeat them, and they're all even  
ArrayList arylist = new ArrayList(); 
//int numCount = 0; 
while (true ) { 
Random ran = new Random(); 
int num = ran.Next(1, 100); 
if (num % 2 == 0 && !arylist.Contains(num)) { // add !arylist.Contains(num) This sentence   Solve the following problems  
arylist.Add(num); // Why run the general display directly 1 The number that satisfies the condition   Step - by - step debugging shows the correct result??  
} 
if (arylist.Count == 10) { 
break ; 
} 
} 
foreach (object item in arylist) { 
Console .WriteLine(item); 
} 
// There are 1 Two strings are separated by Spaces 1 Series of integers, write 1 A program prints out the integers by rearranging them as follows: odd on the left and even on the right. Such as' 2 7 8 3 22 9' To display the ' 7 3 9 2 8 22 
string str = "2 7 8 3 22 9" ; 
ArrayList ary3 = new ArrayList(); 
ArrayList ary4 = new ArrayList(); 
string [] s = str.Split(' ' ); 
foreach (var item in s) { 
if (Convert .ToInt32(item) % 2 == 0) { 
ary4.Add(item); 
} else { 
ary3.Add(item); 
} 
} 
ary3.AddRange(ary4); // because ary1 A type of object  So it can't be used string Of the class join Method to implement character concatenation   We learned later that you can handle generic collections  
string newstr = ary3[0].ToString();// Easy way to get rid of Spaces  
for (int i = 1; i < ary3.Count; i++) { 
newstr += " " + ary3[i]; 
} 
Console .WriteLine(" Original string: {0} , the filtered string {1}" , str, newstr + "test" ); 
Console .Read(); 
} 
} 
} 

Related articles: