C#語法學習一(Array,ArrayList)

/*
 * 
 *1,配置--修改電腦的系統環境變數
 *我的電腦->內容->高級->環境變數中的系統變數中的path後面加上";"號再加上"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727"後這時候在cmd環竟下就可以直接使用csc來編譯.

 *
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2008/8/26
 * Time: 上午 08:48
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/

using System;
namespace Athrun
{
    
class Run
    {
        
static void Main()
        {
            Console.WriteLine(
"Hello World!");
        }
    }
}
 
//數組的介紹
using System;
class Test
{
    
static void Main()
    {
        
//數組的聲明方式
        
//int[] arr=new int[] {1,2,3};
        int[] arr=new int[3];
        arr[
0]=0;
        arr[
1]=1;
        arr[
2]=2;
        
//兩种不同的方式來讀取數組內的元素
        for (int i=0;i<arr.Length;i++)
            System.Console.WriteLine(arr[i]);
        
//foreach(int i in arr)
            
//System.Console.WriteLine(i);
    }
}

//在方法內對數組進行操作
using System;
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]);
    }
    
int i=1
;
    
while (i>0
)
    {
        Console.WriteLine(
"Please enter the array's Length:"
);
        i
=
Int32.Parse(Console.ReadLine());
        PrintArr(i);
    }
}

 

 //在另一個類中對數組進行操作
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]);
    }
}
class Test
{
    
//Main()程序的入口方法
    static void Main()
    {
        
int i=1;
        SetArray arr
=new SetArray();
        
while (i>0)
        {
            Console.WriteLine(
"Please enter the array's Length:");
            i
=Int32.Parse(Console.ReadLine());
            arr.PrintArr(i);
        }
    }
}
 
//ArrayList動態數組
using System;
using System.Collections;
class Athrun
{
    
static void Main()
    {
        ArrayList arr
=new ArrayList();
        
string str1;
        
while(true)
        {
            Console.WriteLine(
"Please add a string to ArrayList:");
            str1
=Console.ReadLine();
            
if(str1=="end")
                
break;
            arr.Add(str1);
            Console.WriteLine();
            
for (int i=0;i<arr.Count;i++)
                Console.Write(
"{0} ",arr[i]);
            Console.WriteLine(
"\n");
        }
    }
}
 
//下面的例子是打印一個數組矩陣
using System;
using System.Collections;
class Matrix
{
    
static void Main()
    {
        
int[,] arr=new int[4,6];
        
for(int i=0;i<4;i++)
        {
            
for(int j=0;j<6;j++)
            {
                arr[i,j]
=(i+1)*10+j+1;
            }
        }
        
for (int i=0;i<4;i++)
        {
            
for(int j=0;j<6;j++)
            {
                Console.Write(
"{0}    ",arr[i,j]);
            }
            Console.WriteLine();
        }
    }
}

//Array和ArrayList的區別
//1,Array的容量是固定的,而ArrayList的容量可根據需要自動擴充.
//2,ArrayList提供添加,插入或移除某一範圍元素的方法.在Array中,
//您只能一次獲取或設置一個元素的值.
//3,Array可以具有多個維度,而ArrayList始終只是一維的.
posted @ 2008-08-27 08:50  Athrun  阅读(545)  评论(0编辑  收藏  举报