使用此代码时,在c#中索引超出范围

使用此代码时,在c#中索引超出范围,c#,C#,如果我有1到100个数字,我应该得到 1--100 2--99 3--98 . .. .. 49---50 代码低于其给定索引超出范围,数组不需要太多维度 static void Main(string[] args) { //// A. 2D array of strings. string[][] a = new string[100][]; int bound0 = a.GetUpperBound(0); int bound1 = a.G

如果我有1到100个数字,我应该得到

1--100 
2--99
 3--98 
.  
.. 
.. 
49---50 
代码低于其给定索引超出范围,数组不需要太多维度

static void Main(string[] args)
{
    //// A. 2D array of strings.
    string[][] a = new string[100][];

    int bound0 = a.GetUpperBound(0);
    int bound1 = a.GetUpperBound(1);
    for (int i = 0; i <= bound0; i++)
    {
        for (int x = 100; x <= bound1; x--)
        {
            string s1 = a[i][x];
            Console.WriteLine(s1);
        }
    }
    Console.WriteLine();
    Console.ReadKey();
}
static void Main(字符串[]args)
{
////A.字符串的二维数组。
字符串[][]a=新字符串[100][];
int bound0=a.GetUpperBound(0);
int bound1=a.GetUpperBound(1);

对于(int i=0;i您需要为数组提供第二个维度。在内部循环中,您减少
循环
变量,而不是增量,这也会导致越界异常。您可能需要知道锯齿数组和二维数组之间的区别。这将解释这一点。 此语句int bound1=a.GetUpperBound(1);由于第二个维度尚未声明,因此给出了异常

使用锯齿阵列

string[][] a = new string[100][];
int bound0 = a.GetUpperBound(0);
for(int i = 0; i <= bound0; i++)
a[i] = new string[3];

for (int i = 0; i <= bound0; i++)
{
        int bound1 = a[i].GetUpperBound(0);
        for (int x = 0; x <= bound1; x++)
        {
            a[i][x] = (i + x).ToString();
            string s1 = a[i][x];
            Console.WriteLine(s1);
        }
 }
string[]a=新字符串[100][];
int bound0=a.GetUpperBound(0);
对于(inti=0;i要使用a.GetUpperBound(1)=>您已经定义了数组的第二维,但没有定义
此外,在内部for循环中,您将递减x(x--)

下面是代码的工作示例,但结果将是空字符串,因为您没有初始化数组

static void Main(string[] args)
{
    //// A. 2D array of strings.
    string[,] a = new string[100, 4];

    int bound0 = a.GetUpperBound(0);
    int bound1 = a.GetUpperBound(1);
    for (int i = 0; i <= bound0; i++)
    {
         for (int x = 0; x <= bound1; x++)
         {
               string s1 = a[i, x];
               Console.WriteLine(s1);
         }
    }
    Console.WriteLine();
    Console.ReadKey();
}
static void Main(字符串[]args)
{
////A.字符串的二维数组。
字符串[,]a=新字符串[100,4];
int bound0=a.GetUpperBound(0);
int bound1=a.GetUpperBound(1);

对于(int i=0;i您没有创建一个2D字符串数组。您创建的是一个字符串数组数组。这意味着“内部”数组可以有任何长度,而“外部”数组当然对此一无所知。因此,要么将数组创建为2D(
新字符串[100,某物]
)或者稍后再询问上限。更好的代码是:

string[][] a = new string[100][];

// Don't forget to create all the subarrays, eg.:
// for (int i = 0; i < a.Length; i ++) a[i] = new string[10];

for (int i = 0; i < a.Length; i++)
{
  for (int j = 0; j < a[i].Length; j++)
  {
    Console.WriteLine(a[i][j]);
  }
}
string[]a=新字符串[100][];
//不要忘记创建所有子阵列,例如:
//对于(inti=0;i
无论如何,如果你说你想做的是真的,那你就是在以一种完全疯狂的方式做这件事。为什么不这样做呢

void PrintDigitPairs(int lowerBound, int upperBound)
{
 for (int i = lowerBound; i <= lowerBound + upperBound / 2; i++)
 {
   Console.WriteLine(i + "-" + (upperBound - i + 1));
 }
}
void PrintDigitPairs(int下限,int上限)
{

对于(int i=lowerBound;i我相信您只希望在
x
大于界限时运行第二个循环:

for (int i = 0; i <= bound0; i++)
{
    //                  | change here from <= to >=
    for (int x = 100; x >= bound1; x--)
    {
        string s1 = a[i][x];
        Console.WriteLine(s1);
    }
}
for(int i=0;i=bound1;x--)
{
字符串s1=a[i][x];
控制台写入线(s1);
}
}

sir再次强调,它的显示数组没有那么多维度。像这样声明数组,string[][]a=newstring[100][100];更重要的是,
a.GetUpperBound(1)
在锯齿状数组中永远无法给出任何有意义的值-数组
a
没有二维,因此它总是抛出。你为什么要这样做?
for(int x=0;x是的,很抱歉是100我打错了
void PrintDigitPairs(int lowerBound, int upperBound)
{
 for (int i = lowerBound; i <= lowerBound + upperBound / 2; i++)
 {
   Console.WriteLine(i + "-" + (upperBound - i + 1));
 }
}
for (int i = 0; i <= bound0; i++)
{
    //                  | change here from <= to >=
    for (int x = 100; x >= bound1; x--)
    {
        string s1 = a[i][x];
        Console.WriteLine(s1);
    }
}