1.代码
Dictionary<int, int> keyValuePairs = new Dictionary<int, int>();
keyValuePairs.Add(i+1, arrayInt[i]);
keyValuePairs[i]
2.代码
using System;
using System.Collections.Generic;
namespace ConsoleApp11
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Dictionary<int, int> keyValuePairs = new Dictionary<int, int>();
int[] arrayInt = { 2, 5, 4, 1, 3 };
for(int i=0;i< arrayInt.Length;i++) {
keyValuePairs.Add(i+1, arrayInt[i]);
}
foreach (int i in arrayInt)
{
Console.WriteLine(keyValuePairs[i]);
}
Console.ReadKey();
}
}
}
3.运行结果

这是一个C#代码示例,演示如何创建一个整数键值对的字典,并通过索引遍历其内容。字典用`Dictionary<int, int>`定义,然后使用`for`循环将数组元素添加到字典中。最后,通过`foreach`循环打印字典的值。
663

被折叠的 条评论
为什么被折叠?



