【问题标题】:Throwing IndexOutOfRangeException抛出 IndexOutOfRangeException
【发布时间】:2013-09-18 03:38:11
【问题描述】:

我对 C# 还很陌生,由于某种原因,我被抛出一个 IndexOutOfRangeException 来处理边界为 0 和 0 的子字符串。

我认为这不是我的范围的问题,因为我已经测试以确保在使用它的地方定义了所有内容。

我正在尝试制作一个非常简单的字谜生成器:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        string[] d = { "Apple", "Bass", "Cat", "Dog", "Ear", "Flamingo", "Gear", "Hat", "Infidel", "Jackrabbit", "Kangaroo", "Lathargic", "Monkey", "Nude", "Ozzymandis", "Python", "Queen", "Rat", "Sarcastic", "Tungston", "Urine", "Virginia", "Wool", "Xylophone", "Yo-yo", "Zebra", " "};
        string var;
        int len = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var = textBox2.Text;
            //textBox1.Text = d[2];
            for (int y = 0; y <= var.Length; y++)
            {
                for (int x = 0; x <= d.Length; x++)
                {
                    if (d[x].Substring(0, 0).ToUpper() == var.Substring(len, len).ToUpper())
                    {
                        textBox1.Text = textBox1.Text + "\n" + d[x];
                        len = len + 1;
                    }
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

【问题讨论】:

  • 数组索引范围从 0..len-1
  • 我想你应该试试 d[x].Substring(0, 1) 代替。
  • 仅供参考,我会避免使用 var 作为变量名,因为它也可以用作隐式类型,例如 var myString = "Hello"
  • 您的代码也不处理var 变量中的空字符串。

标签: c#


【解决方案1】:

您正试图在两个地方读取数组末尾的内容:

for (int y = 0; y <= var.Length; y++)  // here (var is a string which is an array of char)
{
    for (int x = 0; x <= d.Length; x++) // and here

数组使用从零开始的索引。因此最后一个元素位于索引位置 [Length-1]。

当您尝试访问位置 [Length] 处的元素时,您会收到 IndexOutOfRangeException。这个位置是最后一个元素。

不要让循环计数器超过 Length-1:

for (int y = 0; y < var.Length; y++)  
{                 
    for (int x = 0; x < d.Length; x++)

【讨论】:

    【解决方案2】:

    从零开始的数组(或从零开始的索引字符串)的上限是长度减一。

    for (int y = 0; y < var.Length; y++)
    {
        for (int x = 0; x < d.Length; x++)
    

    【讨论】:

    • 谢谢,修复了异常。
    【解决方案3】:

    在基于 的索引中,您不能在端点上建立索引,因为那会超出范围。对于长度 10,您必须从 0-9 迭代:

    for (int y = 0; y < var.Length; y++)
    {
        for (int x = 0; x < d.Length; x++)
        {
            if (d[x].Substring(0, 0).ToUpper() == var.Substring(len, len).ToUpper())
            {
                textBox1.Text = textBox1.Text + "\n" + d[x];
                len = len + 1;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多