Csharp/C Sharp/Collections Data Structure/List
Содержание
Add Item to a list object
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
Collection<Customer> collCustList = new Collection<Customer>();
collCustList.Add(new Customer(99, "Happy Gillmore"));
collCustList.Add(new Customer(77, "Billy Madison"));
List<Customer> rangeList = new List<Customer>();
rangeList.Add(new Customer(55, "Bobby Boucher"));
rangeList.Add(new Customer(44, "Robbie Hart"));
List<Customer> masterList = new List<Customer>(collCustList);
masterList.AddRange(rangeList);
masterList.Insert(2, new Customer(33, "Longfellow Deeds"));
masterList[3] = new Customer(88, "Sonny Koufax");
foreach (Customer cust in masterList)
Console.Out.WriteLine(cust);
}
}
Change Contents
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
List<Customer> collCustList = new List<Customer>();
collCustList.Add(new Customer(99, "H", "P"));
collCustList.Add(new Customer(77, "B", "G"));
collCustList.Add(new Customer(55, "B", "G"));
collCustList.Add(new Customer(88, "B", "P"));
collCustList.Add(new Customer(11, "L", "O"));
List<string> custNames = collCustList.ConvertAll<string>(delegate(Customer cust) {
string retVal = cust.Name;
if (cust.Rating.Equals("P"))
retVal = cust.Name + " ****";
else if (cust.Rating.Equals("G"))
retVal = cust.Name + " ***";
else if (cust.Rating.Equals("O"))
retVal = cust.Name + " **";
return retVal;
});
foreach (string custName in custNames)
Console.Out.WriteLine(custName);
string[] deepNameCopy = new string[custNames.Count];
custNames.CopyTo(deepNameCopy);
custNames[0] = "CHANGED NAME";
foreach (String custName in custNames)
Console.Out.WriteLine(custName);
foreach (string custName in deepNameCopy)
Console.Out.WriteLine(custName);
collCustList.ForEach(delegate(Customer cust) {
cust.Name = cust.Name.ToUpper();
});
foreach (Customer cust in collCustList)
Console.Out.WriteLine(cust.Name);
}
}
Get the size of a list
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
int capacity = 2;
List<Customer> custList = new List<Customer>(capacity);
for (int idx = 1; idx <= 12; idx++) {
custList.Add(new Customer(idx, "Customer" + idx));
if (custList.Capacity > capacity) {
Console.Out.WriteLine("Current Count: {0}", custList.Count);
Console.Out.WriteLine("Old Capacity : {0}", capacity);
Console.Out.WriteLine("New Capacity : {0}", custList.Capacity);
Console.Out.WriteLine("");
capacity = custList.Capacity;
}
}
Console.Out.WriteLine("Final Count : {0}", custList.Count);
Console.Out.WriteLine("Final Capacity : {0}", custList.Capacity);
custList.TrimExcess();
Console.Out.WriteLine("After TrimToSize: {0}", custList.Capacity);
}
}
new List<AssemblyName>() Add(assembly1) Indexer
using System;
using System.Reflection;
using System.Collections.Generic;
class MainClass {
public static void Main(string[] args) {
AssemblyName assembly1 = new AssemblyName("com.microsoft.crypto, Culture=en, PublicKeyToken=a5d015c7d5a0b012, Version=1.0.0.0");
List<AssemblyName> assemblyList = new List<AssemblyName>();
assemblyList.Add(assembly1);
AssemblyName ass2 = assemblyList[0];
Console.WriteLine("\nFound AssemblyName in list: {0}", ass2);
}
}
Remove Item from a List
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
List<Customer> collCustList = new List<Customer>();
collCustList.Add(new Customer(99, "H", "P"));
collCustList.Add(new Customer(77, "B", "G"));
collCustList.Add(new Customer(55, "B", "G"));
collCustList.Add(new Customer(44, "R", "O"));
collCustList.Add(new Customer(22, "H", "D"));
collCustList.Add(new Customer(88, "B", "P"));
collCustList.Add(new Customer(11, "L", "O"));
collCustList.RemoveAt(6);
collCustList.RemoveRange(3, 2);
int numRemoved = collCustList.RemoveAll(delegate(Customer cust) {
return cust.Rating.Equals("Gold");
});
foreach (Customer cust in collCustList)
Console.Out.WriteLine(cust);
collCustList.Clear();
}
}
Reverse Contents
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
List<Customer> collCustList = new List<Customer>();
collCustList.Add(new Customer(99, "H", "P"));
collCustList.Add(new Customer(77, "B", "G"));
collCustList.Add(new Customer(55, "B", "G"));
collCustList.Add(new Customer(88, "B", "P"));
collCustList.Add(new Customer(11, "L", "O"));
Console.Out.WriteLine("Before:");
foreach (Customer cust in collCustList)
Console.Out.WriteLine(cust);
collCustList.Sort(delegate(Customer cust1, Customer cust2) {
return Comparer<int>.Default.rupare(cust1.Id, cust2.Id);
});
Console.Out.WriteLine("After:");
foreach (Customer cust in collCustList)
Console.Out.WriteLine(cust);
collCustList.Reverse();
Console.Out.WriteLine("Reversed:");
foreach (Customer cust in collCustList)
Console.Out.WriteLine(cust);
}
}
Search Test
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
public class Customer : System.IComparable {
private int _id;
private string _name;
private string _rating;
private static SortOrder _order;
public enum SortOrder {
Ascending = 0,
Descending = 1
}
public Customer(int id, string name)
: this(id, name, "Other") {
}
public Customer(int id, string name, string rating) {
this._id = id;
this._name = name;
this._rating = rating;
}
public int Id {
get { return this._id; }
set { this._id = value; }
}
public string Name {
get { return this._name; }
set { this._name = value; }
}
public string Rating {
get { return this._rating; }
set { this._rating = value; }
}
public static SortOrder Order {
get { return _order; }
set { _order = value; }
}
public override bool Equals(Object obj) {
bool retVal = false;
if (obj != null) {
Customer custObj = (Customer)obj;
if ((custObj.Id == this.Id) &&
(custObj.Name.Equals(this.Name) &&
(custObj.Rating.Equals(this.Rating))))
retVal = true;
}
return retVal;
}
public override string ToString() {
return this._id + ": " + this._name;
}
public int CompareTo(Object obj) {
switch (_order) {
case SortOrder.Ascending:
return this.Name.rupareTo(((Customer)obj).Name);
case SortOrder.Descending:
return (((Customer)obj).Name).rupareTo(this.Name);
default:
return this.Name.rupareTo(((Customer)obj).Name);
}
}
}
public class CollectionTest {
public static void Main() {
List<Customer> collCustList = new List<Customer>();
collCustList.Add(new Customer(99, "H", "P"));
collCustList.Add(new Customer(77, "B", "G"));
collCustList.Add(new Customer(55, "B", "G"));
collCustList.Add(new Customer(44, "R", "O"));
collCustList.Add(new Customer(22, "H", "D"));
collCustList.Add(new Customer(88, "B", "P"));
collCustList.Add(new Customer(11, "L", "O"));
int targetId = 22;
Customer cust22 = collCustList.Find(delegate(Customer cust) {
return cust.Id == targetId;
});
Console.Out.WriteLine("Find Customer Id 22: {0}", cust22.Name);
int custIndex = collCustList.FindIndex(delegate(Customer cust) {
return cust.Id == targetId;
});
Console.Out.WriteLine("Find Customer Id 22 Index: {0}", custIndex);
List<Customer> goldCustomers = collCustList.FindAll(delegate(Customer cust) {
return cust.Rating.Equals("Gold");
});
foreach (Customer cust in goldCustomers)
Console.Out.WriteLine("Gold Customer Found: {0}", cust);
Customer platCust = collCustList.FindLast(delegate(Customer cust) {
return cust.Rating.Equals("Platinum");
});
Console.Out.WriteLine("Find Last Platinum Customer: {0}", platCust);
IEnumerable<Customer> rangeCust = collCustList.GetRange(3, 3);
foreach (Customer cust in rangeCust)
Console.Out.WriteLine("Range Customer: {0}", cust);
}
}