用下面的代码创建的一个以1为底,长度是2的数组:
Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
如何把它转成强类型的int数组?附:
int[] x = array as int[];
的结果为null

解决方案 »

  1.   

    先转成object类型,再转成其他类型试一下
      

  2.   

    其他类型指什么类型?
    int[]的话,还是null
      

  3.   


                Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
                int[] x = new int[2];
                array.CopyTo(x, 0);
      

  4.   

           array.CopyTo(x, 0);
      

  5.   

    明确一下吧,
    2维数组的情况下:
    Array array = Array.CreateInstance(typeof(int), new int[] { 2, 2 }, new int[] { 1, 1 });
    int[,] x = array as int[,];
    int[1,1] = 1;
    int[1,2] = 2;
    int[2,1] = 3;
    int[1,2] = 4;
    那么,改成1维数组怎么写
    int[] x = array as int[]x是null
      

  6.   

    必须得用copy吧。C#当中的数组是一个对象,二维数组是对象嵌套对象,他们的数据内存位置并不是连续的。但如果要转化成一维数组,数据存储的位置是连续的,也就是他们的数据位置必须发生转变才行。不像C++,数组只是一个数据存储起始位置的指针,二维数组很容易转化为一维的。
      

  7.   

    copy就是另一个实例了,同步两边的值也是一件非常麻烦的事情,所以不希望新建一个数组实例你说的是类似int[][]的情况吧,类似int[,]的情况不同
      

  8.   


    using System;
    public class SamplesArray  {   public static void Main()  {      // Creates and initializes a multidimensional Array of type String.
          int[] myLengthsArray = new int[2] { 3, 5 };
          int[] myBoundsArray = new int[2] { 2, 3 };
          Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
          for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
             for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ )  {
                int[] myIndicesArray = new int[2] { i, j };
                myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
             }      // Displays the lower bounds and the upper bounds of each dimension.
          Console.WriteLine( "Bounds:\tLower\tUpper" );
          for ( int i = 0; i < myArray.Rank; i++ )
             Console.WriteLine( "{0}:\t{1}\t{2}", i, myArray.GetLowerBound(i), myArray.GetUpperBound(i) );      // Displays the values of the Array.
          Console.WriteLine( "The Array contains the following values:" );
          PrintValues( myArray );
       }
       public static void PrintValues( Array myArr )  {
          System.Collections.IEnumerator myEnumerator = myArr.GetEnumerator();
          int i = 0;
          int cols = myArr.GetLength( myArr.Rank - 1 );
          while ( myEnumerator.MoveNext() )  {
             if ( i < cols )  {
                i++;
             } else  {
                Console.WriteLine();
                i = 1;
             }
             Console.Write( "\t{0}", myEnumerator.Current );
          }
          Console.WriteLine();
       }
    }
    /* 
    This code produces the following output.Bounds:    Lower    Upper
    0:    2    4
    1:    3    7
    The Array contains the following values:
        23    24    25    26    27
        33    34    35    36    37
        43    44    45    46    47
    */
    你可以看看这个。
      

  9.   

    设计模式中有一个叫遍历器模式(interator),大概是说封装对容器的访问。
    你这个问题其实也可以用这个思想来找到一种凑合的解决方法,就是不动原来的这个二维数组,但是用一个遍历器类去访问他。我写个大概的代码吧。pubic class 一维数组
    {
    int[,] 二维数组;
    int row,column;
     public int this[int index]
            {
                get
                {
                    if (index < 0 || index >= row * column)
                        throw ArrayOutOfIndex;//数组越界这个异常是这个名字嘛?                return 二维数组[index/row ][index%row ];
                }
            }
    }///这样你在别的地方使用就可以这么写了:
    一维数组[i]= .....
      

  10.   

    copy以后再把指针赋值过去不就成了
      

  11.   

    又是copy,都说了不要回复copy
      

  12.   

    请比较这两段代码运行的结果:
    Array array = Array.CreateInstance(typeof(int), new int[] { 2, 2 }, new int[] { 1, 1 });
    int[,] x = array as int[,];
    x[1,1] = 1;
    x[1,2] = 2;
    x[2,1] = 3;
    x[1,2] = 4;
    Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
    int[] y = array as int[];
    y[1] = 1;
    y[2] = 2;
      

  13.   

    copy就是另一个实例了,同步两边的值也是一件非常麻烦的事情,所以不希望新建一个数组实例 楼主的意思是不转化成int数组后,int数组中的值发生变化,array里的值也放生变化呢?如果是这样的话。。我没想到什么好的方法。array本身是一个实例,在内存分配地址,
    int数据和array不是一种类型,也没有什么继承关系,所以要用int必须重新生成对象。运行库不能对这样的数组找相同内存地址
    个人意见哈哈
      

  14.   

    27楼的哥们在说梦话。。
    楼主我刚试了你说的这个问题。我找到了原因            Array arr = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] {0 });            int[] nArr = arr as int[];重要的是我标注为红字部门1维数组的下限只能是0,而你写成了1。。给我分啊我 要穷死了。。啊哈哈
      

  15.   

    28楼的哥们在说梦话。。
    要建个下限是0的1维数组的,直接new int[5]就可以了
    还要绕一个大圈用Array.CreateInstance来创建实例吗?1维数组的下限只能是0,用下限是1创建后,看看你的arr是不是null
      

  16.   

    研究了好久,放弃了.这应该是微软的一个BUG吧
    不过我15楼的重载下标运算符的方法应该可以跳过这个问题,就是用户输入x[i]的时候,遍历器返回一个x[i-1],这样也可以给人改变了下标起始位置的错觉.
      

  17.   

    Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
    int[] x = new int[2];
    array.CopyTo(x, 0);