.NET中的数据结构

来源:互联网 发布:qq群淘宝客优惠劵软件 编辑:程序博客网 时间:2024/04/30 03:59

一  Array 

 class Program
    
{
        
static void Main(string[] args)
        
{
            Employee[] arrRDCenter 
= new Employee[4];
            arrRDCenter[
0= new Employee("qiuwh""rd");
            arrRDCenter[
1= new Employee("yuanlh""rd");
            arrRDCenter[
2= new Employee("hongzq""rd");
            arrRDCenter[
3= new Employee("zhumd""rd");
            Console.WriteLine(
"Employees in arrRDCenter:");
            
foreach (Employee emp in arrRDCenter)
            
{
                
if (emp != null) Console.WriteLine(emp.ToString());
            }

            Console.WriteLine();
            
Array Resize
           
            Console.ReadLine();
        }

    }

二 ArrayList

 class Program
    
{
        
static void Main(string[] args)
        
{
            ArrayList arrRDCenter 
= new ArrayList();
            arrRDCenter.Add(
new Employee("qiuwh""rd"));
            arrRDCenter.Add(
new Employee("yuanlh""rd"));
            arrRDCenter.Add(
new Employee("hongzq""rd"));
            arrRDCenter.Add(
new Employee("zhumd""rd"));
           
            Console.WriteLine(
"Employees in arrRDCenter:");
            
foreach (Employee emp in arrRDCenter)
            
{
                
if (emp != null) Console.WriteLine(emp.ToString());
            }

            Console.WriteLine();
            Employee emp1 
= new Employee("luoyq""rd");
            Employee emp2 
= new Employee("liusj""rd");
            arrRDCenter.Add(emp1);
            arrRDCenter.Add(emp2);
            arrRDCenter.Remove(emp1);
            Console.WriteLine(
"Employees in arrRDCenter:");
            
foreach (Employee emp in arrRDCenter)
            
{
                
if (emp != null) Console.WriteLine(emp.ToString());
            }

            Console.ReadLine();
        }

    }

三 GenericList

class Program
    
{
        
static void Main(string[] args)
        
{
            
//ListDemo();
            ArrayListDemo();
            Console.ReadLine();
        }

        
static void ListDemo()
        
{
            List
<Employee> RDCenter = new List<Employee>();
            RDCenter.Add(
new Employee("qiuwh""rd"));
            RDCenter.Add(
new Employee("yuanlh""rd"));
            
//RDCenter.Add("zhumd");
            foreach (Employee emp in RDCenter)
            
{
                
if (emp != null) Console.WriteLine(emp.ToString());
            }

        }

        
static void ArrayListDemo()
        
{
            ArrayList RDCenter 
= new ArrayList();
            RDCenter.Add(
new Employee("qiuwh""rd"));
            RDCenter.Add(
new Employee("yuanlh""rd"));
            RDCenter.Add(
"zhumd");
            
foreach (Employee emp in RDCenter)
            
{
                
if (emp != null) Console.WriteLine(emp.ToString());
            }

        }

    }

另附Employee类

 class Employee
    
{
        
public string Name;
        
public string Dept;
        
public Employee(string name, string dept)
        
{
            
this.Name = name;
            
this.Dept = dept;
        }

        
public override string ToString()
        
{
            
return string.Format("Name:{0}, Dept:{1}"this.Name, this.Dept);
        }

    }

}

四   HashTable

 class Program
    
{
        
static void Main(string[] args)
        
{
            Hashtable currencies 
= new Hashtable();
            currencies.Add(
"PRC""Yuan");
            currencies.Add(
"US""Dollar");
            currencies.Add(
"Japan""Yen");
            currencies.Add(
"France""Euro");
            Console.WriteLine(
"PRC Currency: {0}", currencies["PRC"]);
            Console.WriteLine(
"France Currency: {0}", currencies["France"]);

            
foreach (DictionaryEntry de in currencies)
            
{
                Console.WriteLine(de.Key 
+ " = " + de.Value);
            }

   
            Console.ReadLine();
        }

    }

五 Stack  and  Queue

class Program
    
{
        
static void Main(string[] args)
        
{
            
//StackDemo();
            QueueDemo();
            Console.ReadLine();
        }

        
static void StackDemo()
        
{
            Stack stack 
= new Stack();
            stack.Push(
"1");
            stack.Push(
"2");
            stack.Push(
"3");
            stack.Push(
"4");
            stack.Push(
"5");
            Console.WriteLine(
"topmost element: " + stack.Peek());
            Console.WriteLine(
"total elements: " + stack.Count);
            
while (stack.Count > 0)
            
{
                Console.Write(
" " + stack.Pop());
            }

        }

        
static void QueueDemo()
        
{
            Queue queue 
= new Queue();
            queue.Enqueue(
"1");
            queue.Enqueue(
"2");
            queue.Enqueue(
"3");
            queue.Enqueue(
"4");
            queue.Enqueue(
"5");
            Console.WriteLine(
"topmost element: " + queue.Peek());
            Console.WriteLine(
"total elements: " + queue.Count);
            
while (queue.Count > 0)
            
{
                Console.Write(
" " + queue.Dequeue());
            }

        }

    }
原创粉丝点击