for循环遍历

FollowMe_0 2010-09-17 08:46:22

Dictionary<int, int> abc
我要用for循环遍历。

如何引用abc中的每一项.用as或者强制装换

比如。
ArrayList(int) a

for(int i=0;i!=a.count;i++)
{
int b =(int) a[i];
}
...全文
211 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
完美算法 2010-09-17
  • 打赏
  • 举报
回复
Dictionary<int, int> abc


for( ... )
{
for(... )
{
int b =(int) a[i];
}
}
luoyuanxd 2010-09-17
  • 打赏
  • 举报
回复
请问怎么回复C# code啊?
gaara777 2010-09-17
  • 打赏
  • 举报
回复
学习中。,,
kveces 2010-09-17
  • 打赏
  • 举报
回复
不用foreach,foreach能修改集合?
兔子-顾问 2010-09-17
  • 打赏
  • 举报
回复
你想如何修改?修改value还是整个删掉?如果你要修改,Dictionary不能满足你要求。你可以写:

Dictionary<int, int> abc;
List<KeyValuePair<int,int>> tempList = abc.ToList();
abc.Clear();
for (int i = 0; i < tempList.Count; i++)
{
//操作tempList
}
foreach (KeyValuePair<int, int> item in tempList)
{
abc.Add(item.Key, item.Value);
}


或是先记录下来要删除的对象

Dictionary<int, int> abc;
List<int> removeList = new List<int>();
foreach (KeyValuePair<int,int> item in abc)
{
if (item.Value == 1)
{
removeList.Add(item.Key);
}
}
//根据队列删除
foreach (int key in removeList)
{
abc.Remove(key);
}
FollowMe_0 2010-09-17
  • 打赏
  • 举报
回复
不用foreach,foreach能修改集合?
FollowMe_0 2010-09-17
  • 打赏
  • 举报
回复
不用foreach,foreach能修改集合?
FollowMe_0 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]
C# code

foreach (KeyValuePair<int, int> kvp in abc)
[/Quote]
不用foreach,foreach能修改集合?
BeckLikeCoding 2010-09-17
  • 打赏
  • 举报
回复
没看懂问题。。。
zjp5186 2010-09-17
  • 打赏
  • 举报
回复
Dictionary<int, int> abc = new Dictionary<int, int>();
abc.Add(1, 1);
abc.Add(2, 2);
abc.Add(3, 3);
foreach (KeyValuePair<int,int > kk in abc)
{
Console.WriteLine(kk);
Console.WriteLine(kk.Key);
Console.WriteLine(kk.Value);
}
Console.ReadLine();
bdmh 2010-09-17
  • 打赏
  • 举报
回复

foreach (KeyValuePair<int, int> kvp in abc)
cq819 2010-09-17
  • 打赏
  • 举报
回复
转换成什么类型??????????????????
shenrujun 2010-09-17
  • 打赏
  • 举报
回复
啥意思啊
haoll714773361 2010-09-17
  • 打赏
  • 举报
回复
学习。。
xzjxylophone 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 bdmh 的回复:]

C# code

foreach (KeyValuePair<int, int> kvp in abc)
[/Quote]
+1

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧