标签:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
object Current { get; }
bool MoveNext();
void Reset();
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();//List<int> list = new List<int>(3)
for (int i = 0; i < 1025; i++)
{
if (list.Count == (list.Capacity))
{
Console.WriteLine("容量使用量:"+list.Count + "/" + list.Capacity);
}
list.Add(i);
}
Console.WriteLine("容量使用量:" + list.Count + "/" + list.Capacity);
list.TrimExcess();//调整容量
Console.WriteLine("容量使用量:" + list.Count + "/" + list.Capacity);
Console.Read();
}
}
List<int> list = new List<int>() { 1,2,3,4,5,6,7,8,9,0};
List<string> list = new List<int>() { "aa", "bb", "cc" };
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>();
list.AddRange(new string[]{"aa","bb","cc"});//添加数组
list.AddRange(list);//添加集合
foreach (string s in list)
{
Console.WriteLine(s);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "aa", "bb", "ff" };
list.Insert(2, "cc");//插入元素
list.InsertRange(3, new string[] { "dd", "ee" });//插入集合
foreach (string s in list)
{
Console.WriteLine(s);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "aa", "bb", "cc" };
Console.WriteLine(list[1]);//访问第1个位置上的元素,下标从0开始
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "aa", "bb", "cc" };
list.ForEach(temp => Console.WriteLine("元素:" + temp));
//也可使用: list.ForEach(Console.WriteLine);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "12", "123", "1234", "12345" };
//删除长度小于4的元素
list.RemoveAll(x => { bool flag = x.Length < 4; return flag; });
list.ForEach(Console.WriteLine);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "12", "123", "1234", "12345" };
//搜索长度小于4的元素
list=list.FindAll(x => { bool flag = x.Length < 4; return flag; });
list.ForEach(Console.WriteLine);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "12", "123", "1234", "12345" };
//按字符串长度排序
list.Sort((x, y) => { return y.Length - x.Length; });
list.ForEach(Console.WriteLine);
Console.Read();
}
}
public sealed delegate TOutput Converter<TInput, TOutput>(TInput from)
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "12", "123", "1234", "12345" };
//把字符串转换成数字(String->int)
List<int> array = list.ConvertAll<int>(temp => Int32.Parse(temp));
array.ForEach(temp => Console.WriteLine((temp + 1)));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() {1,3,5,7,9};
IEnumerable<string> array = list.Select<int, string>(x => (x * x).ToString());
foreach(string s in array)
{
Console.WriteLine(s);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string> { "1", "12", "123", "1234" };
//查询长度小于3的元素
IEnumerable<string> query = list.Where(temp => temp.Length < 3);
foreach (string s in query)
{
Console.WriteLine(s);
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
Queue<string> queue = new Queue<string>();
for (int i = 1; i < 5; i++)
{
queue.Enqueue(i.ToString());//在尾部添加元素
}
PrintQueue(queue);//输出元素
string s = queue.Dequeue();//读取并删除头部元素
Console.WriteLine("读取并删除头部元素:" + s);
queue.Enqueue("5");//在尾部添加元素
s = queue.Peek();//读取头部元素
Console.WriteLine("读取头部元素:" + s);
PrintQueue(queue);//输出元素
Console.Read();
}
//输出元素
private static void PrintQueue(Queue<string> q)
{
IEnumerable<string> list = q.Where(t => true);
foreach (string t in list)
{
Console.WriteLine(t);
}
}
}
class Program
{
static void Main(string[] args)
{
Stack<string> stack = new Stack<string>();
for (int i = 1; i < 5; i++)
{
stack.Push(i.ToString());//入栈
}
PrintStack(stack);//输出元素
string s = stack.Pop();//读取并删除栈顶元素
Console.WriteLine("读取并删除栈顶元素:" + s);
s = stack.Peek();//只读取栈顶元素
Console.WriteLine("只读取栈顶元素:" + s);
PrintStack(stack);//输出元素
Console.Read();
}
//输出元素
private static void PrintStack(Stack<string> s)
{
IEnumerable<string> list = s.Where(t => true);
foreach (string t in list)
{
Console.WriteLine(t);
}
}
}
class Program
{
static void Main(string[] args)
{
LinkedList<int> link = new LinkedList<int>();
LinkedListNode<int> node;
for (int i = 0; i < 10; i++)
{
node = new LinkedListNode<int>(i);
if (i % 2 == 0)
{
link.AddFirst(node);//从头部添加节点
}
else
{
link.AddLast(node);//从尾部添加节点
}
}
PrintLink(link);//输出元素
Console.Read();
}
//输出元素
private static void PrintLink(LinkedList<int> l)
{
IEnumerable<int> list = l.Where(t => true);
foreach (int t in list)
{
Console.WriteLine(t);
}
}
}
class Program
{
static void Main(string[] args)
{
SortedList<string, string> sort = new SortedList<string, string>();
sort.Add("K1", "V1");//Add方法添加元素
sort["K2"] = "V2";//索引器添加元素
PrintSorted(sort);//输出元素
Console.WriteLine("是否存在[K3]:" + sort.ContainsKey("K3"));
string value = null;
Console.WriteLine("是否存在[K2]:" + sort.TryGetValue("K2", out value));
Console.WriteLine("[K2]的值:" + value);
Console.Read();
}
//输出元素
private static void PrintSorted(SortedList<string, string> s)
{
IEnumerable<KeyValuePair<string, string>> list = s.Where(t => true);
foreach (KeyValuePair<string, string> t in list)
{
Console.WriteLine(t.Key + " ---> " + t.Value);
}
}
}
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "1","12","123","1234","a","ab","abc"};
var lookup=list.ToLookup(x => x.Length);//根据元素的长度分组
foreach (var t in lookup)
{
List<string> temp=t.ToList<string>();//把一组数据转换成集合
temp.ForEach(x=>Console.Write(x+"、"));//输出集合
Console.WriteLine();
}
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
ObservableCollection<int> list = new ObservableCollection<int>();
list.CollectionChanged += ListCollectionChanged;//添加事件方法
list.Add(10);//产生事件
list.Add(20);//产生事件
list.Remove(10);//产生事件
Console.Read();
}
//事件方法
private static void ListCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Console.WriteLine("本次修改的方式:" + e.Action.ToString());
if (e.OldItems != null)
{
Console.WriteLine("本次修改的位置:" + e.OldStartingIndex);
Console.WriteLine("本次修改的元素个数:" + e.OldItems.Count);
}
if (e.NewItems != null)
{
Console.WriteLine("本次修改的位置:" + e.NewStartingIndex);
Console.WriteLine("本次修改的元素个数:" + e.NewItems.Count);
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
BitArray barray = new BitArray(20);
barray.SetAll(true);//对所有位赋值
PrintBitArray(barray);//输出
barray.Set(1, false);
barray[2] = false;
PrintBitArray(barray);//输出
Console.Read();
}
//输出函数
private static void PrintBitArray(BitArray b)
{
foreach (bool bit in b)
{
Console.Write(bit ? 1 : 0);
}
Console.WriteLine();
}
}
class Program
{
static void Main(string[] args)
{
BitVector32 b32 = new BitVector32();
PrintBitVector32(b32);//输出位
int bit = BitVector32.CreateMask();
for (int i = 0; i < 4; i++)
{
b32[bit] = true;
Console.WriteLine(bit);
PrintBitVector32(b32);//输出位
b32[bit] = false;
bit = BitVector32.CreateMask(bit);
}
Console.WriteLine(b32.Data);//输出数
Console.Read();
}
//输出函数
private static void PrintBitVector32(BitVector32 b32)
{
for (int i = 0; i < 32; i++)
{
Console.Write(b32[i] ? 1 : 0);
}
Console.WriteLine();
}
}
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4315209.html