迭代器
就是用來遍歷的
引例:自己做一個List叫MyList,實現一個迭代器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
class MyList
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
private int Index=-1;
public bool MoveNext()
{
Index++;
return Index < Nums.Length;
}
public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
while (List.MoveNext())
{
Console.WriteLine(List.Current);
}
Console.ReadLine();
}
}
}
image.png
現在要求打印兩次元素,加一個要求例如生成的三個數為1,2,3
想要輸出:
1
——1
——2
——3
2
——1
——2
——3
3
——1
——2
——3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
class Enumerator//定義一個迭代器
{
private int[] Nums=null;
public Enumerator(int[] nums)
{
this.Nums = nums;
}
private int Index = -1;
public bool MoveNext()
{
Index++;
return Index < Nums.Length;
}
public int Current { get { return Nums[Index]; } }
}
class MyList
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
public Enumerator GetEnumerator()
{
return new Enumerator(Nums) ;
}
//private int Index=-1;
//public bool MoveNext()
//{
// Index++;
// return Index < Nums.Length;
//}
//public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
Enumerator e1 = List.GetEnumerator();
while (e1.MoveNext())
{
Console.WriteLine(e1.Current);
Enumerator e2 = List.GetEnumerator();
while (e2.MoveNext())
{
Console.WriteLine("---" + e2.Current);
}
}
Console.ReadLine();
}
}
}
image.png
C#已經將迭代器模式已經進行進一步封裝
集合
集合就是實現了IEnumerable的接口
下面我們來實現IEnumerable接口的自定義的集合
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Text5
{
class Program
{
//class Enumerator//定義一個迭代器
//{
// private int[] Nums=null;
// public Enumerator(int[] nums)
// {
// this.Nums = nums;
// }
// private int Index = -1;
// public bool MoveNext()
// {
// Index++;
// return Index < Nums.Length;
// }
// public int Current { get { return Nums[Index]; } }
//}
class MyList:IEnumerable <int>
{
private int[] Nums { get; set; }
public MyList(int n)
{
var r = new Random();
Nums = new int[n];
for (var i = 0; i < n; i++)
{
Nums[i] = r.Next(0, 10);
}
}
public IEnumerator<int> GetEnumerator()
{
foreach (var item in Nums )
{
yield return item;
}
//throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
//throw new NotImplementedException();
}
//public Enumerator GetEnumerator()
//{
// return new Enumerator(Nums) ;
//}
//private int Index=-1;
//public bool MoveNext()
//{
// Index++;
// return Index < Nums.Length;
//}
//public int Current { get { return Nums[Index]; } }
}
static void Main(string[] args)
{
MyList List = new MyList(5);
foreach (var i1 in List)
{
Console.WriteLine(i1);
foreach (var i2 in List)
{
Console.WriteLine("---" + i2);
}
}
//var e1 = List.GetEnumerator();
//while (e1.MoveNext())
//{
// Console.WriteLine(e1.Current);
// var e2 = List.GetEnumerator();
// while (e2.MoveNext())
// {
// Console.WriteLine("---" + e2.Current);
// }
//}
Console.ReadLine();
}
}
}
image.png
foreach 實現里的接口
常用集合
List<>
Dictionary<>
Strack<>
Queue<>
LinkedList<>
static void Main(string[] args)
{
var dic = new Dictionary<int, string>();
dic.Add(1, "張三");
dic.Add(2, "李四");
dic.Add(3, "張三");
dic.Add(4, "張三");
foreach (var item in dic)
{
Console.WriteLine( item.Key.ToString());
Console.WriteLine( item.Value);
}
Console.ReadLine();
}
image.png