二维数组 [英] two dimentional array

查看:110
本文介绍了二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 错了吗?
如果不是,为什么最后一行有错误?
错误:索引超出了数组的范围.

Hi Is it wrong?
If not why is there an error in the last line?
error:Index was outside the bounds of the array.

const int n = 5;
        int[,] next=new int[n,n];

            for(int i=0;i<=n;i++)
                for (int j = 0; j <= n; j++)
                    next[i,j]= -1;

推荐答案

我认为
for(int i=0;i<=n;i++)
                for (int j = 0; j <= n; j++)


将是


will be

for(int i=0;i<n;i++)
                for (int j = 0; j < n; j++)



从0到5表示其取值为6倍.但是数组声明为最大大小为5.



For 0 to 5 means its taking value for 6 times .But array is declared with max size of 5.


您正在创建一个二维数组,其中第一个维保存的数组为5整数,但如果您使用i = 0; i < = 5您实际上创建了6个整数(从0开始),因此基本上,您试图将6个整数拟合到声明仅容纳5的数组中.要么声明n = 6或使用i = 0;我在循环中< 5.希望这会有所帮助.
You are creating a two dimensional array, with the first dimension holding an array of 5 integers, but if you use i = 0; i <= 5 you actually create 6 integers (starts with 0), and so basically, you are trying to fit 6 integers into an array that was declared to hold only 5. Either declare n = 6, or use i = 0; i < 5 in your loops. Hope this helps.


这篇关于二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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