16 个解决方案
#1
int[] arr3 = { 4, 5, 6, 6, 7, 2, 5, 6 };
int index = 0;
foreach (var c in arr3)
Console.WriteLine(index++);
#2
正解
#3
定义起始位置,+1就好了。
#4
这个行
#5
foreach语句只能另加index了
#6
http://discuss.joelonsoftware.com/default.asp?dotnet.12.612943.11
A foreach uses the IEnumerator interface, which has a Current property, and MoveNext and Reset methods. So there's not even the concept of an index in it. (Also, there's no guarantee what order you're enumerating in.) You'll need to either do your own counting, or use a for loop instead if the collection can take an index.
A foreach uses the IEnumerator interface, which has a Current property, and MoveNext and Reset methods. So there's not even the concept of an index in it. (Also, there's no guarantee what order you're enumerating in.) You'll need to either do your own counting, or use a for loop instead if the collection can take an index.
#7
恩,貌似是这样地
#8
1楼正解
#9
虽然可以自己加索引变量...但是需要索引器就不应该用foreach应该用for...
#10
1.另弄一个变量计数.
2.用for改写.
2.用for改写.
#11
正解一楼~~
#12
不另外定义计数变量index,List<>就没有直接的属性或方法记录循环当前下标吗
#13
那自己写个CLASS,
一个记INDEX,一个记数
一个记INDEX,一个记数
#14
for(int i=0;i<arr3.count;i++)
{
/////你要的索引
Console.WriteLine(i);
}
#15
个人感觉foreach的本意就是遍历,而不关心下标
如果需要就要自己计数
如果需要就要自己计数
#16
可以用Array.IndexOf()获得位置
#1
int[] arr3 = { 4, 5, 6, 6, 7, 2, 5, 6 };
int index = 0;
foreach (var c in arr3)
Console.WriteLine(index++);
#2
正解
#3
定义起始位置,+1就好了。
#4
这个行
#5
foreach语句只能另加index了
#6
http://discuss.joelonsoftware.com/default.asp?dotnet.12.612943.11
A foreach uses the IEnumerator interface, which has a Current property, and MoveNext and Reset methods. So there's not even the concept of an index in it. (Also, there's no guarantee what order you're enumerating in.) You'll need to either do your own counting, or use a for loop instead if the collection can take an index.
A foreach uses the IEnumerator interface, which has a Current property, and MoveNext and Reset methods. So there's not even the concept of an index in it. (Also, there's no guarantee what order you're enumerating in.) You'll need to either do your own counting, or use a for loop instead if the collection can take an index.
#7
恩,貌似是这样地
#8
1楼正解
#9
虽然可以自己加索引变量...但是需要索引器就不应该用foreach应该用for...
#10
1.另弄一个变量计数.
2.用for改写.
2.用for改写.
#11
正解一楼~~
#12
不另外定义计数变量index,List<>就没有直接的属性或方法记录循环当前下标吗
#13
那自己写个CLASS,
一个记INDEX,一个记数
一个记INDEX,一个记数
#14
for(int i=0;i<arr3.count;i++)
{
/////你要的索引
Console.WriteLine(i);
}
#15
个人感觉foreach的本意就是遍历,而不关心下标
如果需要就要自己计数
如果需要就要自己计数
#16
可以用Array.IndexOf()获得位置