怎么用For循环啊??、

FollowMe_0 2010-09-17 08:21:29


//foreach (KeyValuePair<int, int> item in childFrm.UserTypeTable)
//{
// foreach(NodeRU ru in shape.NodeRU)
// {
// if (item.Key == ru.user.id)
// {
// childFrm.UserTypeTable[item.Key] = ru.user.type;
// }
// }

//}
集合修改异常。

怎么用For循环啊??、
我想循环的时候修改值
...全文
268 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyd021 2010-09-17
  • 打赏
  • 举报
回复
foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容以避免产生不可预知的副作用

作为一个字典类型的集合,key是不能更改的。
tanbin_0521 2010-09-17
  • 打赏
  • 举报
回复

for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
  • 打赏
  • 举报
回复
嵌套了
geminizane 2010-09-17
  • 打赏
  • 举报
回复
-,-有点没看懂..
完美算法 2010-09-17
  • 打赏
  • 举报
回复
foreach (KeyValuePair<int, int> item in childFrm.UserTypeTable)
{
foreach(NodeRU ru in shape.NodeRU)
{
if (item.Key == ru.user.id)
{
childFrm.UserTypeTable[item.Key] = ru.user.type;
}
}
}
参加判断的的值(红色),付给了新的值,循环是不是要重新游历?
鉴于外循环是int[],建议改用for循环加if判断,内循环还用foreach

这样也比较啰嗦,
建议
如果是数据库就用查询,
如果不是数据库就用linQ吧
gaara777 2010-09-17
  • 打赏
  • 举报
回复
贴全点代码。。。才好解。。
完美算法 2010-09-17
  • 打赏
  • 举报
回复
看懂了,有两个集合,
当一个集合中的元素个另外一个集合中的元素属性相等时执行:
childFrm.UserTypeTable[item.Key] = ru.user.type;

嵌套循环肯定是可以的
一般不用2个foreach
建议用2个for,或用foreach嵌套for
完美算法 2010-09-17
  • 打赏
  • 举报
回复
楼主的问题不清晰:
用的是foreach,提的问题却是for
阿彪兄 2010-09-17
  • 打赏
  • 举报
回复

for(var i=0i<childFrm.UserTypeTable.count;i++)
{

}
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();
ysz89757 2010-09-17
  • 打赏
  • 举报
回复
可以结贴了
zhujunyanging 2010-09-17
  • 打赏
  • 举报
回复
for(int j=0;j<childFrm.Count;j++)
{
for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
}
Ryan20082009 2010-09-17
  • 打赏
  • 举报
回复


for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
FollowMe_0 2010-09-17
  • 打赏
  • 举报
回复
Dictionary<int, int>UserTypeTable

题目看清楚,是外层的KeyValuePair<int, int> item in childFrm.UserTypeTable



怎么取出每项。我as不了。。。
zcfzcf001 2010-09-17
  • 打赏
  • 举报
回复
int sm;
for(int i=0;i<=100;i++)
{
sm=sm+1;
}
y_zp2010 2010-09-17
  • 打赏
  • 举报
回复
楼上正解
FollowMe_0 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luqc1985 的回复:]
for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
[/Quote]

写错了你。。

不可以的
浪子-无悔 2010-09-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 luqc1985 的回复:]

for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
[/Quote]
楼上正解。
别样苍茫 2010-09-17
  • 打赏
  • 举报
回复
for(int i=0;i<shape.集合行数;i++)
{
if (shape[i].Key == ru.user.id)
{

}
}
zhouxingyu896 2010-09-17
  • 打赏
  • 举报
回复
可以看C#2008和.NET 3.5平台 这本书
上面有详细的介绍
加载更多回复(2)

111,129

社区成员

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

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

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