Csharp/CSharp Tutorial/Data Structure/ArrayList object

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Add object with IComparable interface implementation to an ArrayList

<source lang="csharp">using System; using System.Collections;

// Implement the non-generic IComparable interface. class Product : IComparable {

 string name; 
 double cost; 
 int onhand; 

 public Product(string n, double c, int h) { 
   name = n; 
   cost = c; 
   onhand = h; 
 } 

 public override string ToString() { 
   return 
     String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}", 
                   name, cost, onhand); 
 } 

 // Implement the IComparable interface. 
 public int CompareTo(object obj) { 
   Product b; 
   b = (Product) obj; 
   return name.rupareTo(b.name); 
 } 

}

class MainClass {

 public static void Main() { 
   ArrayList inv = new ArrayList(); 
    
   // Add elements to the list 
   inv.Add(new Product("A", 5.5, 3)); 
   inv.Add(new Product("B", 8.9, 2));    
   inv.Add(new Product("C", 3.0, 4)); 
   inv.Add(new Product("D", 1.8, 8)); 

   Console.WriteLine("Product list before sorting:"); 
   foreach(Product i in inv) { 
     Console.WriteLine("   " + i); 
   } 
   Console.WriteLine(); 

   // Sort the list. 
   inv.Sort(); 

   Console.WriteLine("Product list after sorting:"); 
   foreach(Product i in inv) { 
     Console.WriteLine("   " + i); 
   } 
 } 

}</source>

Product list before sorting:
   A         Cost:  $5.50  On hand: 3
   B         Cost:  $8.90  On hand: 2
   C         Cost:  $3.00  On hand: 4
   D         Cost:  $1.80  On hand: 8
Product list after sorting:
   A         Cost:  $5.50  On hand: 3
   B         Cost:  $8.90  On hand: 2
   C         Cost:  $3.00  On hand: 4
   D         Cost:  $1.80  On hand: 8

Add reference objects to ArrayList

<source lang="csharp">using System; using System.Collections; using System.Collections.Specialized; class MyClass{

  public string MyName="";   

} class MainClass {

 static void Main(string[] args)
 {
   ArrayList classList = new ArrayList();
   classList.AddRange(new MyClass[] { new MyClass(), 
                                          new MyClass(), 
                                          new MyClass()});
   Console.WriteLine("Items in List: {0}", classList.Count);
   // Print out current values. 
   foreach(MyClass c in classList)
   {
     Console.WriteLine("MyClass name: {0}", c.MyName);  
   }
 }

}</source>

Items in List: 3
MyClass name:
MyClass name:
MyClass name:

Add user-defined object to an ArrayList

<source lang="csharp">using System; using System.Collections;

class Product {

 string name; 
 double cost; 
 int onhand; 

 public Product(string n, double c, int h) { 
   name = n; 
   cost = c; 
   onhand = h; 
 } 

 public override string ToString() { 
   return 
     String.Format("{0,-10}Cost: {1,6:C}  On hand: {2}", 
                   name, cost, onhand); 
 } 

}

class MainClass {

 public static void Main() { 
   ArrayList inv = new ArrayList(); 
    
   // Add elements to the list 
   inv.Add(new Product("A", 5.9, 3)); 
   inv.Add(new Product("B", 8.2, 2));    
   inv.Add(new Product("C", 3.5, 4)); 
   inv.Add(new Product("D", 1.8, 8)); 

   Console.WriteLine("Product list:"); 
   foreach(Product i in inv) { 
     Console.WriteLine("   " + i); 
   } 
 } 

}</source>

Product list:
   A         Cost:  $5.90  On hand: 3
   B         Cost:  $8.20  On hand: 2
   C         Cost:  $3.50  On hand: 4
   D         Cost:  $1.80  On hand: 8

ArrayList and your own object: Add objects to ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

}

class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979

ArrayList and your own object: Get an enumerator using the GetEnumerator() method and use it to read the object elements in ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

} class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   IEnumerator myEnumerator = myArrayList.GetEnumerator();
   while (myEnumerator.MoveNext())
   {
     Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
   }
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myEnumerator.Current = Name is M, Number is 2001
myEnumerator.Current = Name is B, Number is 2001
myEnumerator.Current = Name is C, Number is 1999
myEnumerator.Current = Name is T, Number is 1979

ArrayList and your own object: remove an object from ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

} class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   myArrayList.Remove(myB);
   DisplayArrayList("myArrayList", myArrayList);
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is C, Number is 1999
myArrayList[2] = Name is T, Number is 1979

ArrayList and your own object: Use the BinarySearch() method to search myArrayList for an object

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

} class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   myArrayList.Sort();
   DisplayArrayList("myArrayList", myArrayList);
   int index2 = myArrayList.BinarySearch(myC);
   Console.WriteLine("Found myC at index " + index2);
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList[0] = Name is T, Number is 1979
myArrayList[1] = Name is C, Number is 1999
myArrayList[2] = Name is M, Number is 2001
myArrayList[3] = Name is B, Number is 2001
Found myC at index 1

ArrayList and your own object: Use the Contains() method to determine if an object is in the ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

}

class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   if (myArrayList.Contains(myB))
   {
     Console.WriteLine("myArrayList does contain myB");
     int index = myArrayList.IndexOf(myB);
     Console.WriteLine("myB occurs at index " + index);
   }
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList does contain myB
myB occurs at index 1

ArrayList and your own object: Use the GetRange() method to get a range of object elements from ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

} class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   ArrayList anotherArrayList = myArrayList.GetRange(1, 2);
   DisplayArrayList("anotherArrayList", anotherArrayList);
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
anotherArrayList[0] = Name is B, Number is 2001
anotherArrayList[1] = Name is C, Number is 1999

ArrayList and your own object: use the IndexOf() method to display the index of an object

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

}

class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   if (myArrayList.Contains(myB))
   {
     Console.WriteLine("myArrayList does contain myB");
     int index = myArrayList.IndexOf(myB);
     Console.WriteLine("myB occurs at index " + index);
   }
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList does contain myB
myB occurs at index 1

ArrayList and your own object: Use the Sort() method to sort objects in the ArrayList

<source lang="csharp">using System; using System.Collections; class Employee : IComparable {

 public string Name;
 public int Number;
 public Employee(string Name, int Number)
 {
   this.Name = Name;
   this.Number = Number;
 }
 public override string ToString()
 {
   return "Name is " + Name + ", Number is " + Number;
 }
 public int Compare(object lhs, object rhs)
 {
   Employee lhsEmployee = (Employee) lhs;
   Employee rhsEmployee = (Employee) rhs;
   if (lhsEmployee.Number < rhsEmployee.Number) {
     return -1;
   } else if (lhsEmployee.Number > rhsEmployee.Number) {
     return 1;
   } else {
     return 0;
   }
 }
 public int CompareTo(object rhs) {
   return Compare(this, rhs);
 }

} class MainClass {

 public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) {
   for (int i = 0; i < myArrayList.Count; i++) {
     Console.WriteLine(arrayListName + "[" + i + "] = " + myArrayList[i]);
   }
 }
 public static void Main()
 {
   ArrayList myArrayList = new ArrayList();
   // add four Employee objects to myArrayList using the Add() method
   Console.WriteLine("Adding four Employee objects to myArrayList");
   Employee myM = new Employee("M", 2001);
   Employee myB = new Employee("B", 2001);
   Employee myC = new Employee("C", 1999);
   Employee myT = new Employee("T", 1979);
   myArrayList.Add(myM);
   myArrayList.Add(myB);
   myArrayList.Add(myC);
   myArrayList.Add(myT);
   DisplayArrayList("myArrayList", myArrayList);
   myArrayList.Sort();
   DisplayArrayList("myArrayList", myArrayList);
 }

}</source>

Adding four Employee objects to myArrayList
myArrayList[0] = Name is M, Number is 2001
myArrayList[1] = Name is B, Number is 2001
myArrayList[2] = Name is C, Number is 1999
myArrayList[3] = Name is T, Number is 1979
myArrayList[0] = Name is T, Number is 1979
myArrayList[1] = Name is C, Number is 1999
myArrayList[2] = Name is M, Number is 2001
myArrayList[3] = Name is B, Number is 2001