用下面的代码创建的一个以1为底,长度是2的数组:
Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
如何把它转成强类型的int数组?附:
int[] x = array as int[];
的结果为null
Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
如何把它转成强类型的int数组?附:
int[] x = array as int[];
的结果为null
int[]的话,还是null
Array array = Array.CreateInstance(typeof(int), new int[] { 2 }, new int[] { 1 });
int[] x = new int[2];
array.CopyTo(x, 0);
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
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
*/
你可以看看这个。
你这个问题其实也可以用这个思想来找到一种凑合的解决方法,就是不动原来的这个二维数组,但是用一个遍历器类去访问他。我写个大概的代码吧。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]= .....
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;
int数据和array不是一种类型,也没有什么继承关系,所以要用int必须重新生成对象。运行库不能对这样的数组找相同内存地址
个人意见哈哈
楼主我刚试了你说的这个问题。我找到了原因 Array arr = Array.CreateInstance(typeof(int), new int[] { 5 }, new int[] {0 }); int[] nArr = arr as int[];重要的是我标注为红字部门1维数组的下限只能是0,而你写成了1。。给我分啊我 要穷死了。。啊哈哈
要建个下限是0的1维数组的,直接new int[5]就可以了
还要绕一个大圈用Array.CreateInstance来创建实例吗?1维数组的下限只能是0,用下限是1创建后,看看你的arr是不是null
不过我15楼的重载下标运算符的方法应该可以跳过这个问题,就是用户输入x[i]的时候,遍历器返回一个x[i-1],这样也可以给人改变了下标起始位置的错觉.
int[] x = new int[2];
array.CopyTo(x, 0);