Code
//數(shù)組的介紹
using System;
class Test
{
static void Main()
{
//數(shù)組的聲明方式
//int[] arr=new int[] {1,2,3};
int[] arr=new int[3];
arr[0]=0;
arr[1]=1;
arr[2]=2;
//兩種不同的方式來讀取數(shù)組內(nèi)數(shù)組內(nèi)的元素
for (int i=0;i<arr.Length;i++)
System.Console.WriteLine(arr[i]);
//foreach(int i in arr)
//System.Console.WriteLine(i);
}
}
Code
//在方法內(nèi)對數(shù)組進(jìn)行操作
using System;
class Test
{
//一個靜態(tài)方法。根據(jù)所輸入的長度來建立一個一維數(shù)組,來進(jìn)行初始化以及在屏幕上打印數(shù)組元素
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);
}
}
Code
//ArrayList動態(tài)數(shù)組
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");
}
}
}
Code
//下面的例子是打印一個一維數(shù)據(jù)矩陣
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();
}
}
}