第二节上(数组)

首先下载 sharpDecelop 中文版。

打开它,文件,新建文件,空白文档。

Empty1.cs
/*
 * Created by SharpDevelop.
 * User: BYW
 * Date: 2009-2-19
 * Time: 0:19
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System ;

class SetArray
{
     
      public  void PrintArr(int ArrLength)
                 {
                                      int [] arr = new int[ArrLength];
                                      for(int i=0;i<arr.Length;i++)
                                               {
                                                     arr[i]=i;
                                               }
                                       Console.WriteLine("Print Array's value");
                                           for(int i=0;i<arr.Length;i++)
                                               {
                                                     Console.WriteLine("arr[{0}]={1}",i,arr[i]); //大括号里面的零 表示逗号里面的第一个变量  1表示逗号里面的第二个变量
                                               }
}


class Test
{
                 static void PrintArr(int ArrLength)
                 {
                                      int [] arr = new int[ArrLength];
                                      for(int i=0;i<arr.Length;i++)
                                               {
                                                     arr[i]=i;
                                               }
                                       Console.WriteLine("Print Array's value");
                                           for(int i=0;i<arr.Length;i++)
                                               {
                                                     Console.WriteLine("arr[{0}]={1}",i,arr[i]); //大括号里面的零 表示逗号里面的第一个变量  1表示逗号里面的第二个变量
                                               }
                 }

 static void Main()
 {
第一个例子  main函数里面
--------------------------------------------------------------------------------------------------------------------------------------
  //int [] arr = new int[] {1,2,3};

                                   // 对于局部变量和域声明,允许一种简写形式,这样就不需要去再声明数组类型。
                                    可简化为:

  //int [] arr = {1,2,3};
                                    
                                    //另一种方法
                                    int [] arr =new int[3]
                                    arr[0]=1;
                                    arr[1]=2;
                                    arr[2]=3;

                                  //一般情况下 对数组的赋值 可用一个循环来完成
                            
                            
                                 // for 循环 做输出
  for(int i=0;i<arr.Length ;i++)
  {
   Console.WriteLine (arr[i]);
  } 
                                 
                                   //foreach 循环 做输出
                                    foreach(int i in arr)
                                    {
                                                      Console.WriteLine (i);
                                    }
--------------------------------------------------------------------------------------------------------------------------------------
第二个例子  main函数里面
--------------------------------------------------------------------------------------------------------------------------------------
                                   //调用同一个类中的方法
                                   int i=1;
                                   while (i>0);
                                   {
                                                  Console.WriteLine("please enter the array's length:");
                                   }
                                    
                                   i=Int32.Parse(Console.ReadLine());     
                                   PrintArr(i); 


                                  //调用SetArray的方法
                                 //如上因为方法不是静态所以必先初始化
                                  SetArray arr = new SetArray();
                                        while (i>0);
                                   {
                                                  Console.WriteLine("please enter the array's length:");
                                   }
                                    
                                   i=Int32.Parse(Console.ReadLine()); 
                                   arr.printArr(i);  //对象.方法    
--------------------------------------------------------------------------------------------------------------------------------------
                                
 }
}

posted @ 2009-02-19 16:25  BeamWen  阅读(124)  评论(0编辑  收藏  举报