首页 新闻 会员 周边

Array类的问题

0
悬赏园豆:5 [已解决问题] 解决于 2013-06-27 10:09

Array是所有数组的基类,为什么不能用其子类实例化Array?Array对象为什么不能用下标索引?

具体到第二行和第四行问题在哪?

Invictus的主页 Invictus | 初学一级 | 园豆:76
提问于:2013-06-27 09:02
< >
分享
最佳答案
0

第二行的问题是 arr2是个对象,Array是个类,不是数组,是类就要实例化,没有实例化不能当数组那么用

 

第三行已经回答了可以用子类实例化Array,你也可以这么写 Array a = new ArrayList[3];

 

第四行的问题是你要用Array应该这么用

public class SamplesArray2{

   public static void Main()  {

      // Creates and initializes a new three-dimensional Array of type Int32.
      Array myArr = Array.CreateInstance( typeof(Int32), 2, 3, 4 );
      for ( int i = myArr.GetLowerBound(0); i <= myArr.GetUpperBound(0); i++ )
         for ( int j = myArr.GetLowerBound(1); j <= myArr.GetUpperBound(1); j++ )
            for ( int k = myArr.GetLowerBound(2); k <= myArr.GetUpperBound(2); k++ )  {
               myArr.SetValue( (i*100)+(j*10)+k, i, j, k );
            }

      // Displays the properties of the Array.
      Console.WriteLine( "The Array has {0} dimension(s) and a total of {1} elements.", myArr.Rank, myArr.Length );
      Console.WriteLine( "\tLength\tLower\tUpper" );
      for ( int i = 0; i < myArr.Rank; i++ )  {
         Console.Write( "{0}:\t{1}", i, myArr.GetLength(i) );
         Console.WriteLine( "\t{0}\t{1}", myArr.GetLowerBound(i), myArr.GetUpperBound(i) );
      }

      // Displays the contents of the Array.
      Console.WriteLine( "The Array contains the following values:" );
      PrintValues( myArr );
   }


   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();
   }
}
收获园豆:5
WuRang | 小虾三级 |园豆:1730 | 2013-06-27 09:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册