无法正确地将值放入双数组中 [英] Not able to correctly put values in a double array

查看:89
本文介绍了无法正确地将值放入双数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我一直在尝试使用双数组进行简单的迭代,但是,我似乎无法将值设置为特定的索引。下面是我使用的代码。我在突出显示的LOC处获得了超出范围异常的索引。



  byte  [] utf8Bytes = Encoding.Convert(unicode,
utf8,
unicodeBytes);


int k = 1 ;

int [,] d = new int [k, 3 ];
int i = 0 ;
int flag = 0 ;
foreach byte b in utf8Bytes)
{


d [k,i] = Convert.ToInt32(b);
i ++;
if (k < 3
{
k ++;
继续;
}




}







我做错了什么?

解决方案

查看你的代码,并注意每次使用 k

  int  k =  1 ; 

int [,] d = new int [k, 3 ];
d [k,i] = Convert.ToInt32(b);
if (k < 3
k ++;



所以你从 k = 1 开始,分配一个数组按三列排...然后尝试访问第二行...

C#数组索引基于零:所以你声明的数组在[0,0],[0]有三个元素,1]和[0,2]


引用:

索引超出范围异常

此错误消息告诉您尝试使用索引超出范围的数组。



这是使用调试器轻松解决的典型问题。



- 在违规行上设置断点

- 在调试器模式下运行程序

- check用于访问数组的变量的值,并与数组的大小进行比较。

- 很快,响铃应该开始响起你的想法。

Hi all,

I have been trying to use a double array for a simple iteration, however, I seem to have trouble setting values to particular indices. Below, is the code I used. I am getting index out of range exception at the highlighted LOC.

byte[] utf8Bytes = Encoding.Convert(unicode,
                                                utf8,
                                                unicodeBytes);

           
           int k = 1;
         
           int[,] d = new int[k, 3];
           int i = 0;
           int flag = 0;
           foreach (byte b in utf8Bytes)
           {
              
               
               d[k, i] = Convert.ToInt32(b);
               i++;
               if (k < 3)
               {
                   k++;
                   continue;
               }

              

              
           }




what is it that I am doing wrong?

解决方案

Look at your code, and pay attention to every use of k:

int k = 1;

int[,] d = new int[k, 3];
    d[k, i] = Convert.ToInt32(b);
    if (k < 3)
        k++;


So you start with k = 1, allocate an array with one row by three columns...and then try to access the second row...
C# array indices are zero based: so the array you declared had three elements at [0, 0], [0, 1], and [0, 2]


Quote:

index out of range exception

This error message tells you that you try to use your array with an index out of range.

This is the typical kind of problem easily solved by using a debugger.

- set a breakpoint on the offending line
- run your program in debugger mode
- check the value of variables used to access your array, and compare against the size of your array.
- soon, a bell should start to ring in your mind.


这篇关于无法正确地将值放入双数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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