索引超出数组(C#)的范围 [英] Index was outside the bounds of the array (C#)

查看:248
本文介绍了索引超出数组(C#)的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这行上得到索引在数组的边界之外",这是怎么回事?

I get "Index was outside the bounds of the array" on this line, what's wrong?

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();
            }

        }
    }

推荐答案

从0而不是1开始对数组进行访问

Start your array accesses from 0, and not 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开始任何循环.使条件小于长度,直到相等.更像是:

Also, start any of your loops at 0, instead of 1. And make the conditional be less than the length, not until equal. More like:

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

这篇关于索引超出数组(C#)的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆