【问题标题】:Foreach going out of bounds while searching through array c#Foreach 在通过数组 c# 搜索时超出范围
【发布时间】:2015-09-18 15:22:51
【问题描述】:

我的程序的目的是获取一个数据 txt 文件并对其进行编辑,和/或对其进行加减。 文本文件格式是这样的:

Name|Address|Phone|# of people coming|isRSVP

我的代码似乎一直在做它的工作,直到我尝试单击 listbox 中的一个名称,它需要搜索多维数组以提取信息并放置在一组可以编辑的文本框中。问题是我使用的 foreach 循环给了我一个 out of bounds 异常。我尝试进行调试以确保数组中的数据正确并查看过程。一切似乎都正确,但由于某种原因,条件语句 person[0]==listbox1.selectedIndex 没有返回 true,即使两者都与我在进入流程的步骤中看到的相同。任何帮助将不胜感激。 这是我的代码:

StringBuilder newFile = new StringBuilder();
static  string txtList= "guest_list.txt";    
static string[] file = File.ReadAllLines(txtList);    
static int x = file.Count();
string[,] list = new string[x ,5];

public void loadGuestList()
{

    int count2 = 0;
    foreach (string line in file)
    {
        string[] sliced = line.Split('|');
        int count = 0;

        list[count2, count] = sliced[0];
        count++;
        list[count2, count] = sliced[1];
        count++;
        list[count2,count] = sliced[2];
        count++;
        list[count2,count]= sliced[3];
        count++;
        list[count2, count] = sliced[4];
        count++;
        listBox1.Items.Add(list[count2,0]);
        count2++;
    }
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (string person in list)
    {
        if (  person[0].ToString()==listBox1.SelectedItem.ToString())
        {
            addTxt.Text = char.ToString(person[1]);
            textPhone.Text = char.ToString(person[2]);
            textPeople.Text = char.ToString(person[3]);
            if (person[4] == 'n' )
            {
            }
            else
            {
                chkRSVP.Checked = true;
            }
            break;
        }
    }
}

【问题讨论】:

  • 您还没有告诉我们异常发生在哪里 - 或者您尝试做些什么来诊断它。如果您可以展示一个简短但完整的程序来演示该问题,那将非常有帮助 - 就像一个控制台应用程序一样。我怀疑当您尝试编写这样的应用程序来隔离问题时,您会发现错误..
  • "statement person[0]==listbox1.selectedIndex is not return true 即使即使两者相同" - 你说 index,在代码中你是使用选定的项目。这会是个问题吗?
  • listBox1.Items[listBox1.SelectedIndex].ToString() ??

标签: c# arrays multidimensional-array foreach textbox


【解决方案1】:

问题出在这一行:

foreach (string person in list)

该列表被定义为string[,],当您为每个结束时将执行每个元素,而不仅仅是数据列。你真的应该这样做:

for(int index = 0; index <= list.GetUpperBound(0); index++)
{
    string slice1 = list[index, 0];
    string slice2 = list[index, 1];
    ....
}

或改用Dictionary&lt;string, string[]&gt;()

【讨论】:

  • 谢谢,我想是我试图让事情变得简单,但最终让它变得更加复杂。我最终使用了一个简单的嵌套 for 循环来遍历多维数组,它完美地工作
【解决方案2】:

尝试使用“Person”对象并覆盖equals()。现在你正试图将你的多维数组 (list[0]) 放入一个字符串中,它会给你一个不需要的结果。您应该改用 list[0,0]。

【讨论】:

    【解决方案3】:

    与 Adam Gritt 一致,我测试了以下代码,它似乎可以工作:

    using System;
    
    namespace so_foreach_bounds
    {
        class MainClass
        {
            public static void Main (string[] args)
            {
                //System.Text.StringBuilder newFile = new System.Text.StringBuilder();
                string txtList= "guest_list.txt";    
                string[] file = System.IO.File.ReadAllLines(txtList);    
                int x = file.Length;
                System.Collections.Generic.List<string[]> list = new System.Collections.Generic.List<string[]> ();
    
                foreach (string line in file)
                {
                    string[] sliced = line.Split('|');
                    list.Add(sliced);
                }
    
                foreach (string[] person in list)
                {
                    Console.WriteLine (String.Join(", ", person));
    
                    if (person[0] =="Gary")
                    {
                        string txtAdd = person[1];
                        string txtPhone = person[2];
                        string txtpeople = person[3];
                        if (person[4] == "n" )
                        {
                        }
                        else
                        {
                            bool has_resvped = true;
                        }
                        break;
                    }
    
                }
            }
        }
    }
    

    问题在于您如何迭代二维数组。创建一个“Person”类通常比尝试通过数组管理它更好。哦,是的,在尝试使用其中一个成员之前检查列表框项是否被选中通常是个好主意。

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 2021-06-20
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 2013-07-05
      • 2010-11-17
      相关资源
      最近更新 更多