日期:2014-05-18  浏览次数:20403 次

声明一个二维数组,长度不可确定总是报错
ArrayList a = new ArrayList();
  int[] b = { 1, 2 };
  int testi;
  int testl;
  int testj;
   
  a.Add(b);
  b[0] = 3;
  b[1] = 4;
  testl=a.Count;
   
  a.Add(b);

  for (testi = 0; testi < testl; testi++)
  {
  for(testj in a[testi])
  {
  Response.Write(a[testi,testj]);
  }
   
  }
我想实现这样的方法,求给出正确答案的方法

------解决方案--------------------
你可以这么写:
C# code
        List<int[]> a = new List<int[]>();
        int[] b = new int[] { 1, 2 };
        a.Add(b);
        b = new int[] { 3, 4 };
        a.Add(b);
        foreach (int[] w in a)
        {
            foreach (int y in w)
                Response.Write(y + "<br/>");
        }

------解决方案--------------------
或者用for循环也可以:
C# code
        List<int[]> a = new List<int[]>();
        int[] b = new int[] { 1, 2 };
        a.Add(b);
        b = new int[] { 3, 4 };
        a.Add(b);
        for (int i = 0; i < a.Count; i++)
        {
            for (int j = 0; j < a[i].Length; j++)
                Response.Write(a[i][j] + "<br/>");
        }

------解决方案--------------------
通过5楼,6楼的帮助看懂了。
试试这样

C# code

List<int[]> a = new List<int[]>( 
   new int[]{1, 2},
   new int[]{3, 4}
  );

// for or foreach ...