Csharp/C Sharp by API/System.Collections.Generic/List — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 15:11, 26 мая 2010
Содержание
extends List
<source lang="csharp"> using System; using System.Collections.Generic; using System.Text; public static class VectorDelegates {
public static int Compare(Vector x, Vector y) { if (x.R > y.R) { return 1; } else if (x.R < y.R) { return -1; } return 0; } public static bool TopRightQuadrant(Vector target) { if (target.Theta >= 0.0 && target.Theta <= 90.0) { return true; } else { return false; } }
} public class Vectors : List<Vector> {
public Vectors() { } public Vectors(IEnumerable<Vector> initialItems) { foreach (Vector vector in initialItems) { Add(vector); } } public string Sum() { StringBuilder sb = new StringBuilder(); Vector currentPoint = new Vector(0.0, 0.0); sb.Append("origin"); foreach (Vector vector in this) { sb.AppendFormat(" + {0}", vector); currentPoint += vector; } sb.AppendFormat(" = {0}", currentPoint); return sb.ToString(); }
} public class Vector {
public double? R = null; public double? Theta = null; public double? ThetaRadians { get { return (Theta * Math.PI / 180.0); } } public Vector(double? r, double? theta) { if (r < 0) { r = -r; theta += 180; } theta = theta % 360; R = r; Theta = theta; } public static Vector operator +(Vector op1, Vector op2) { try { double newX = op1.R.Value * Math.Sin(op1.ThetaRadians.Value) + op2.R.Value * Math.Sin(op2.ThetaRadians.Value); double newY = op1.R.Value * Math.Cos(op1.ThetaRadians.Value) + op2.R.Value * Math.Cos(op2.ThetaRadians.Value); double newR = Math.Sqrt(newX * newX + newY * newY); double newTheta = Math.Atan2(newX, newY) * 180.0 / Math.PI; return new Vector(newR, newTheta); } catch { return new Vector(null, null); } } public static Vector operator -(Vector op1) { return new Vector(-op1.R, op1.Theta); } public static Vector operator -(Vector op1, Vector op2) { return op1 + (-op2); } public override string ToString() { string rString = R.HasValue ? R.ToString() : "null"; string thetaString = Theta.HasValue ? Theta.ToString() : "null"; return string.Format("({0}, {1})", rString, thetaString); }
} class Program {
static void Main(string[] args) { Vectors route = new Vectors(); route.Add(new Vector(2.0, 90.0)); route.Add(new Vector(1.0, 180.0)); route.Add(new Vector(0.5, 45.0)); route.Add(new Vector(2.5, 315.0)); Console.WriteLine(route.Sum()); Comparison<Vector> sorter = new Comparison<Vector>(VectorDelegates.rupare); route.Sort(sorter); Console.WriteLine(route.Sum()); Predicate<Vector> searcher = new Predicate<Vector>(VectorDelegates.TopRightQuadrant); Vectors topRightQuadrantRoute = new Vectors(route.FindAll(searcher)); Console.WriteLine(topRightQuadrantRoute.Sum()); }
}
</source>
List.Add
<source lang="csharp"> using System; using System.Collections.Generic; public class MainClass {
static void Main() { List<int> intList = new List<int>(); intList.Add( 1 ); intList.Add( 2 ); intList.Add( 3 ); intList.Add( 4 ); foreach( int n in CreateReverseIterator(intList) ) { Console.WriteLine( n ); } } static IEnumerable<T> CreateReverseIterator<T>( IList<T> list ) { int count = list.Count; for( int i = count-1; i >= 0; --i ) { yield return list[i]; } }
}
</source>
List.AsReadOnly()
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { List<int> intList = new List<int>(new int[] { 3, 5, 15, 1003, 25 }); ReadOnlyCollection<int> roList = intList.AsReadOnly(); }
}
</source>
List.Capacity
<source lang="csharp"> using System; using System.Collections.Generic;
class MainClass {
public static void Main() { List<char> lst = new List<char>(); Console.WriteLine("Initial number of elements: " + lst.Count); Console.WriteLine(); Console.WriteLine("Adding 6 elements"); lst.Add("C"); lst.Add("A"); lst.Add("E"); lst.Add("B"); lst.Add("D"); lst.Add("F"); Console.WriteLine("Number of elements: " + lst.Count); Console.WriteLine("Adding 20 more elements"); // Add enough elements to force lst to grow. for(int i=0; i < 20; i++) lst.Add((char)("a" + i)); Console.WriteLine("Current capacity: " + lst.Capacity); Console.WriteLine("Number of elements after adding 20: " + lst.Count); Console.Write("Contents: "); foreach(char c in lst) Console.Write(c + " "); Console.WriteLine("\n"); }
}
</source>
List.ConvertAll
<source lang="csharp">
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { List<string> stringList1 = new List<string>(new string[] { "99", "182", "15" }); List<int> intList1 = stringList1.ConvertAll<int>(Convert.ToInt32); }
}
</source>
List.Count
<source lang="csharp">
using System; using System.Collections.Generic;
class MainClass {
public static void Main() { List<char> lst = new List<char>(); Console.WriteLine("Initial number of elements: " + lst.Count); Console.WriteLine(); Console.WriteLine("Adding 6 elements"); lst.Add("C"); lst.Add("A"); lst.Add("E"); lst.Add("B"); lst.Add("D"); lst.Add("F"); Console.WriteLine("Number of elements: " + lst.Count); Console.Write("Current contents: "); for(int i=0; i < lst.Count; i++) Console.Write(lst[i] + " "); Console.WriteLine("\n"); }
}
</source>
List.ForEach
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; public class MainClass {
public static void Main() { List<int> intList = new List<int>(new int[] { 3, 5, 15, 1003, 25 }); intList.ForEach(delegate(int x) { Console.WriteLine(x); }); }
}
</source>
List.Remove()
<source lang="csharp"> using System; using System.Collections.Generic;
class MainClass {
public static void Main() { List<char> lst = new List<char>(); Console.WriteLine("Initial number of elements: " + lst.Count); Console.WriteLine(); Console.WriteLine("Adding 6 elements"); lst.Add("C"); lst.Add("A"); lst.Add("E"); lst.Add("B"); lst.Add("D"); lst.Add("F"); Console.WriteLine("Number of elements: " + lst.Count); Console.WriteLine("Removing 2 elements"); // Remove elements from the array list. lst.Remove("F"); lst.Remove("A"); Console.WriteLine("Number of elements: " + lst.Count); }
}
</source>
new List<T>()
<source lang="csharp"> using System.Collections.Generic; using System;
class MainClass {
public static void Main(string[] args) { List<MyClass> list = new List<MyClass>(); list.Add(new MyClass()); MyClass ass2 = list[0]; Console.WriteLine("\nFound in list: {0}", ass2); }
} class MyClass {
public override string ToString(){ return "my class"; }
}
</source>