C#语法学习一(Array,ArrayList)
Code
//在另一个类中对数组进行操作
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);
}
}
}
Code
//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");
}
}
}
Code
//下面的例子是打印一个一维数据矩阵
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();
}
}
}
[第1页][第2页]