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];
}
...全文
242 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
内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多类型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统中的建模与求解实践,帮助用户掌握MOPSO在实际工程问题中的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。

111,129

社区成员

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

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

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