【问题标题】:Index was outside the bounds of the array (C#)索引超出了数组的范围(C#)
【发布时间】:2011-09-07 16:38:40
【问题描述】:

我在这一行得到“索引超出了数组的范围”,这是怎么回事?

Kort[x, y] = Sort[x] + Valor[y] + " ";    

下面是完整代码:

    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 uppgift_13
    {
        public partial class Form1 : Form
        {
            string[,] Kort = new string[4,13];
            string[] Valor = new string[13];
            string[] Sort = new string[4];
            int x, y;


            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                Valor[1] = "2";
                Valor[2] = "3";
                Valor[3] = "4";
                Valor[4] = "5";
                Valor[5] = "6";
                Valor[6] = "7";
                Valor[7] = "8";
                Valor[8] = "9";
                Valor[9] = "10";
                Valor[10] = "Knekt";
                Valor[11] = "Dam";
                Valor[12] = "Kung";
                Valor[13] = "Ess";

                Sort[1] = "H";
                Sort[2] = "R";
                Sort[3] = "S";
                Sort[4] = "K";
            }


            private void LaddaKort()
            {
                for (this.x = 1; this.x <= 4; this.x++)
                {
                    for (this.y = 1; this.y <= 13; this.y++)
                    {
                        Kort[x, y] = Sort[x] + Valor[y] + " ";
                    }
                }
            }

            private void SkrivKort()
            {
                for (this.x = 1; this.x <= 4; this.x++)
                {
                    for (this.y = 1; this.y <= 13; this.y++)
                    {
                        richTextBox1.AppendText(Kort[x, y]);
                    }
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                LaddaKort();
                SkrivKort();
            }

        }
    }

【问题讨论】:

标签: c# arrays


【解决方案1】:

从 0 开始您的数组访问,而不是 1

所以,改成这样:

private void Form1_Load(object sender, EventArgs e)
            {
                Valor[0] = "2";
                Valor[1] = "3";
                Valor[2] = "4";
                Valor[3] = "5";
                Valor[4] = "6";
                Valor[5] = "7";
                Valor[6] = "8";
                Valor[7] = "9";
                Valor[8] = "10";
                Valor[9] = "Knekt";
                Valor[10] = "Dam";
                Valor[11] = "Kung";
                Valor[12] = "Ess";

                Sort[0] = "H";
                Sort[1] = "R";
                Sort[2] = "S";
                Sort[3] = "K";
            }

另外,从 0 而不是 1 开始任何循环。并使条件小于长度,直到相等。更像:

for (int i=0; i < theArray.Length; i++)

【讨论】:

  • 您还应该提及他的循环边界,因为那是他将要寻找的地方。它们也是基于 1 的......编辑:我看到你更新了帖子:)
  • 是的,感谢您指出这一点,不幸的是,在我点击提交后意识到这一点!
【解决方案2】:

在 C# 中,数组是从零开始的...

看看 Kevek 给你的回答加:

这个:

for (this.x = 1; this.x <= 4; this.x++)
{
  for (this.y = 1; this.y <= 13; this.y++)
  ...

应该是:

for (this.x = 0; this.x < 4; this.x++)
{
  for (this.y = 0; this.y < 13; this.y++)
  ...

【讨论】:

    【解决方案3】:

    Sort 是一个数组 0..3,Valor 是 0..12。所以你不能使用 Sort[4] 和 Valor[13]。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多