首页 > 代码库 > C# 数组 二维数组

C# 数组 二维数组

数组:相同数据类型的元素按一定顺序排列的集合。是一组变量

作用:操作大量数据   
数组的定义
1、数组里面的内容必须是同一类型
2、数据必须有长度限制                               //  定义时限定长度,往后无法更改

 

一维数组

表达式

1数据类型[ ] 变量名 = new 数据类型[长度];              string [ ] s  = new atring [ 5 ];    //定义一个有五个变量的string类型数组

                                                                             s [0] = "aaa";                             //索引 0 所对应的字符串值是 aaa

                                                                             s [1] = "bbb";                            

                                                                               ......                                          //最多定义五个

 

2、数据类型[ ]变量名= new 数据类型[ ] { "  ", "  ", "  ", "  " };                                         //花括号有多少后面中括号就默认多少。

3 、string[] sss = new string[5] { "aaa", "bbb", "ccc", "ddd", "eee" };

 

一维数组的赋值:   变量名[索引] = 值;

一维数组的取值:   变量 = 变量名[索引];                      int[  ] a = new int[] { 1,5,7};

                                                                             int b = a[ 0 ];                                       //赋值

                                                                             a[0] = a[a.Length - 1];

                                                                             a[a.Length - 1] = b;

                                                                              for (int i = 0; i < a.Length; i++)

                                                                                      Console.WriteLine(  a[i]  );            // 取值

案例一

      将用户输入的内容放入数组中并输出。

                        string [ ] ss = new string [ 5 ] ;

                       for ( int i = 0 ; i < ss.Length ; i++ );

                      {

                             Console.Write("请输入第“+ i +1+”个内容");

                            ss [ i ] = Console.ReadLie();

                       }

                       for ( int i = 0 ; i < ss.Length ; i++ );

                      {

                           Console.WriteLine( ss [ i ] );

                       }

案例二

       简单的抽奖设计

                string [ ] sss = new string [ ] {"升官","发财","娶老婆","换车","换房","换新颜"};

                Random r = new Random();

                int a = r . Next( 0, sss.Langth );

               Console.WriteLine( sss[ a ] );

案例三

         让数据停一会

             for (int i =0 ; i<10 ;i++);

             Console.WriteLine( i ); 

             System.Threading.Thread.sleep(500)              //让数字有时间间隔的打印出来    括号内为毫秒  1000毫秒= 1 秒                            

              Console.Clear();                                               //见打印结果清除    

 

二维数组:
表达式:

     string[ , ] 名字 = new string[ 2 , 3 ];                      //1个二维数组有2个一维数组,每一个一维数组中有3个变量
     接赋值 { {"","",""} ,{"","a",""} }                                                            
      名字[1,1]

                                                                              //在二维数组中每一个一维数组的长度必须是一样的

                 strinr [ , ] s2 = new string [ 2,3 ]

                 {{a,b,c,},{aa,bb,cc}};                            //   s2.Longth ------计算数组中有几个变量

三维数组:
表达式:

    string[,,] 名字 = new string[2,3,4];
                                                                              //有2个二维数组,每一个二维数组中有3个一维数组,每一个一维数组中有4个变量
  
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

                                                                                     

 

C# 数组 二维数组