在我的转换程序中,我使用字典为从预定义文本文件中读取的特定行指定名称。然后创建一个名为" Rows"的多维数组。发生错误:
已添加具有相同键的项目。
编写以下代码:
Dictionary<string, int> rows = new Dictionary<string, int>();
// The file "read.txt" is being read
string[] lines = File.ReadAllLines("read.txt");
int[] array = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// In this section each line is being read and the spacing is removed
foreach (string s in lines)
{
string[] arr = s.Split(' '); // This line ables the program to differ between variables and numbers.
for (int i = 0; i < arr.Length; i++)
{
array[i] = Convert.ToInt32(arr[i]); // arr is now converted into a Int.
rows.Add("array" + i, array[i]);
}
}
然后调试错误,然后循环结束。为什么会出现错误以及如何解决?
答案 0 :(得分:3)
如果您有多行,则会再次从"array" + i
== array0
开始。该项已经添加到字典中,因此例外。
你必须记住对行进行编号,所以也许你应该像"array" + "line" + lineNumber + "row" + i
那样格式化它。
答案 1 :(得分:0)
Dictionary只允许出现一次键,错误意味着你要两次添加相同的键。
你的for循环插入"array" + i
作为键。在foreach循环的第二次运行中,i再次为0,因此您实际上插入了"item0"
两次,并且正在获得异常。
答案 2 :(得分:0)
这是寻求全功能解决方案的用户。 通过结合其他答案,我得到了一个功能代码。它返回它想要做的事情。
namespace test_read_txt
{
class Program
{
static void Main(string[] args)
{
// A string is created for the 14 first element
Dictionary<string, int[]> rows = new Dictionary<string, int[]>();
// The file "read.txt" is being read
string[] lines = File.ReadAllLines("read.txt");
// In this section each line is being read and the spaceing is removed
int counter = 0;
foreach (string s in lines)
{
//Console.WriteLine(s);
string[] arr = s.Split(' '); // This line ables the program to differ between variables and numbers.
int[] array = new int[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
array[i] = Convert.ToInt32(arr[i]); // arr is now converted into a Int.
}
string key = "array_" + counter++;
rows.Add(key, array);
//ShowArray(array);
}
foreach (string key in rows.Keys)
{
Console.WriteLine($"{key}: {String.Join(" ", rows[key])}");
}
Console.ReadLine();
}
public static void ShowArray(int[] arr) {
foreach (var item in arr) {
Console.Write(item + "-");
}
Console.WriteLine();
}
}
}
读取文件是一个看起来像这样的文本文件,它位于bin - &gt;调试程序文件夹:
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Output是每一行,作为int [],名称为array_0 ... array12。享受!!
答案 3 :(得分:-1)
您可能希望将多个 int与词典中的键相关联。字典不会为此做,而是
Dictionary<string, List<int>> or Dictionary<string, HashSet<int>>
如果您使用(“array”+ i)作为键,则int会执行此操作。
初始化一个有很多0的数组并不是很好,而是使用new int [n]。
通过这些小改动,这是我的更新版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, HashSet<int>> rows = new Dictionary<string, HashSet<int>>();
// The file "read.txt" is being read
string[] lines = new string[] { "1", "2", "1 2"}; //File.ReadAllLines("read.txt");
// In this section each line is being read and the spacing is removed
foreach (string s in lines)
{
string[] arr = s.Split(' '); // This line ables the program to differ between variables and numbers.
int[] array = new int[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
array[i] = Convert.ToInt32(arr[i]); // arr is now converted into a Int.
string key = "array" + i;
if (!rows.ContainsKey(key)) rows.Add(key, new HashSet<int>());
rows[key].Add(array[i]);
}
}
foreach (string key in rows.Keys)
{
Console.WriteLine($"{key}: {String.Join(", ", rows[key])}");
}
}
}
}
可能会有许多进一步的改进。