C# 更改每个订单?
从列表的末尾到开头,而不是从开头到结尾,是否还有其他方法可以进行遍历(最好不要对列表重新排序) 编辑:如果您专门处理C# 更改每个订单?,c#,foreach,C#,Foreach,从列表的末尾到开头,而不是从开头到结尾,是否还有其他方法可以进行遍历(最好不要对列表重新排序) 编辑:如果您专门处理列表,还有一个步骤。该类定义了自己的Reverse方法,其签名与可枚举.Reverse扩展方法不同。在这种情况下,您需要将变量引用“提升”到IEnumerable: 您可以使用常规for循环,从末尾开始递减,而不是从顶部开始递增 比如: for(int i=foo.lenth; i != 0; i--) { do stuff } 您可能不想做任何复杂的事情,所以我建议只使用for
列表
,还有一个步骤。该类定义了自己的Reverse
方法,其签名与可枚举.Reverse
扩展方法不同。在这种情况下,您需要将变量引用“提升”到IEnumerable
:
您可以使用常规for循环,从末尾开始递减,而不是从顶部开始递增 比如:
for(int i=foo.lenth; i != 0; i--)
{
do stuff
}
您可能不想做任何复杂的事情,所以我建议只使用for循环 但是,如果它是某种需求,您当然可以为自定义列表迭代行为提供支持。不是c,但您也可以这样做:-)
这取决于你所说的列表是什么意思
?不,除非使用Linq及其Reverse()函数列表
- 你的定制系列?很容易,只需像您一样实现IEnumerator 需要
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class ReversedEnumerator : IEnumerator<int>
{
List<int> v;
int index;
public ReversedEnumerator(List<int> v) {
this.v = v;
this.index = v.Count;
}
public int Current
{
get { return v[index]; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return v[index]; }
}
public bool MoveNext()
{
return --index >= 0;
}
public void Reset()
{
index = this.v.Count;
}
}
class EnumeratorStub : IEnumerable<int>
{
List<int> v;
public EnumeratorStub(List<int> v)
{
this.v = v;
}
public IEnumerator<int> GetEnumerator()
{
return new ReversedEnumerator(v);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new ReversedEnumerator(v);
}
}
class Program
{
static EnumeratorStub Reverse(List<int> v)
{
return new EnumeratorStub(v);
}
static void Main(string[] args)
{
List<int> v = new List<int>();
v.Add(1);
v.Add(2);
v.Add(3);
foreach (int item in Reverse(v))
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间控制台应用程序3
{
类ReversedEnumerator:IEnumerator
{
清单五;
整数指数;
公共反向计数器(列表v){
这个,v=v;
该指数=v.计数;
}
公共电流
{
获取{return v[index];}
}
公共空间处置()
{
}
对象System.Collections.IEnumerator.Current
{
获取{return v[index];}
}
公共图书馆
{
返回——索引>=0;
}
公共无效重置()
{
索引=此v.计数;
}
}
类枚举器桶:IEnumerable
{
清单五;
公共普查员(名单五)
{
这个,v=v;
}
公共IEnumerator GetEnumerator()
{
返回新的反向计数器(v);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回新的反向计数器(v);
}
}
班级计划
{
静态枚举器(列表v)
{
返回新的枚举数(v);
}
静态void Main(字符串[]参数)
{
列表v=新列表();
v、 增加(1);
v、 增加(2);
v、 增加(3);
foreach(反面的int项目(v))
{
控制台写入线(项目);
}
Console.ReadKey();
}
}
}
我建议重构代码示例以使用泛型。这样,您就可以将其用于任何容器类型;
IList<String> strList = new IList<String>();
strList.Add("A");
strList.Add("B");
strList.Add("C");
for (int i = strList.Count-1; i>=0;i--)
{
Console.WriteLine(strList[i]);
}
strList.添加(“A”);
strList.添加(“B”);
strList.添加(“C”);
对于(int i=strList.Count-1;i>=0;i--)
{
Console.WriteLine(strList[i]);
}
未尝试,但应能正常工作。注意,这将创建另一个列表并使用两倍的内存,因此对于大型列表,您可能需要使用不同的策略。但总的来说,这是最好的方法。@Fredou:你说得对,编译器会抱怨。对于
List
您必须执行source.aseneumerable().Reverse()
@Fredou:我将OP对“List”的引用作为一个抽象概念,映射到IEnumerable
。如果问题被改为专门针对列表
,我将更改我的答案:-)@Bryan Watts,也许,也许你可以在当前答案下添加一个关于列表的小注释:-)@Fredou:有一个,注释!哈哈,说真的,不过我会加上一个脚注。部分事实。仅当集合可索引(实现IList或提供索引器成员)时,才能使用索引。foreach可以处理任何实现IEnumerable的东西,IEnumerable不提供索引。从技术上讲,它不是foreach,但我喜欢这个解决方案,因为它不涉及创建另一个内存结构。不幸的是,如果枚举没有要递减的一致索引器,它将不起作用。
Dim a As New List(Of Integer)
a.Add(1)
a.Add(2)
a.Add(3)
For Each i In a.AsEnumerable.Reverse
Debug.Print(i)
Next
using System;
using System.Collections.Generic;
namespace ConsoleApplication3
{
class ReversedEnumerator : IEnumerator<int>
{
List<int> v;
int index;
public ReversedEnumerator(List<int> v) {
this.v = v;
this.index = v.Count;
}
public int Current
{
get { return v[index]; }
}
public void Dispose()
{
}
object System.Collections.IEnumerator.Current
{
get { return v[index]; }
}
public bool MoveNext()
{
return --index >= 0;
}
public void Reset()
{
index = this.v.Count;
}
}
class EnumeratorStub : IEnumerable<int>
{
List<int> v;
public EnumeratorStub(List<int> v)
{
this.v = v;
}
public IEnumerator<int> GetEnumerator()
{
return new ReversedEnumerator(v);
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return new ReversedEnumerator(v);
}
}
class Program
{
static EnumeratorStub Reverse(List<int> v)
{
return new EnumeratorStub(v);
}
static void Main(string[] args)
{
List<int> v = new List<int>();
v.Add(1);
v.Add(2);
v.Add(3);
foreach (int item in Reverse(v))
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
IList<String> strList = new IList<String>();
strList.Add("A");
strList.Add("B");
strList.Add("C");
for (int i = strList.Count-1; i>=0;i--)
{
Console.WriteLine(strList[i]);
}