欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# Rank属性与GetUpperBound方法的深入分析

程序员文章站 2023-12-19 18:37:40
array的rank 属性:   语法:public int rank { get; }   得到array的秩(维数)。array...
array的rank 属性:
   语法:public int rank { get; }   得到array的秩(维数)。
array而getupperbound 方法:
   语法:public int getupperbound(int dimension)  用于获取 array 的指定维度的上限。
--------------------------------------------------------------------------------
示例:
复制代码 代码如下:

using system;
 public class samplesarray  {
    public static void main()  {
       // creates a new one-dimensional array of type int32.
       array my1dintarray = array.createinstance( typeof(int32), 5 );
       // uses getlowerbound and getupperbound in the for loop.
       for ( int i = my1dintarray.getlowerbound(0); i <= my1dintarray.getupperbound(0); i++ )
          my1dintarray.setvalue( i+1, i );
       // displays the bounds and values of the one-dimensional array.
       console.writeline( "one-dimensional array:" );
       console.writeline( "rank/tlower/tupper" );
       console.writeline( "{0}/t{1}/t{2}", 0, my1dintarray.getlowerbound(0), my1dintarray.getupperbound(0) );
       console.writeline( "values:" );
       printvalues( my1dintarray );
       console.writeline();
       // creates a new three-dimensional array of type int32.
       array my3dintarray = array.createinstance( typeof(int32), 2, 3, 4 );
       // uses getlowerbound and getupperbound in the for loop.
       for ( int i = my3dintarray.getlowerbound(0); i <= my3dintarray.getupperbound(0); i++ )
          for ( int j = my3dintarray.getlowerbound(1); j <= my3dintarray.getupperbound(1); j++ )
             for ( int k = my3dintarray.getlowerbound(2); k <= my3dintarray.getupperbound(2); k++ )  {
                my3dintarray.setvalue( (i*100)+(j*10)+k, i, j, k );
             }
       // displays the bounds and values of the multidimensional array.
       console.writeline( "multidimensional array:" );
       console.writeline( "rank/tlower/tupper" );
       for ( int i = 0; i < my3dintarray.rank; i++ )
          console.writeline( "{0}/t{1}/t{2}", i, my3dintarray.getlowerbound(i), my3dintarray.getupperbound(i) );
       console.writeline( "values:" );
       printvalues( my3dintarray );
    }
    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.
 one-dimensional array:
 rank    lower    upper
 0    0    4
 values:
     1    2    3    4    5
 multidimensional array:
 rank    lower    upper
 0    0    1
 1    0    2
 2    0    3
 values:
     0    1    2    3
     10    11    12    13
     20    21    22    23
     100    101    102    103
     110    111    112    113
     120    121    122    123
*/

上一篇:

下一篇: