一、一维数组的创建与遍历
方法一:
类型[ ]变量=new 类型[ ];l例如:string a=new string[ var int]
若使用new关键字,则方括号内必须声明一个int类型的值来表示长度
也可以:string[ ] a=new string[ ]{ }
方法二:
string[ ] a={"a","b","c","d"};
一维数组的遍历:for和foreach
using System;
namespace practice3
{
class MainClass
{
public static void Main( string [] args)
{
string [] a = new string []{ "huang" , "jun" , "kai" , "h" , "j" , "k" };
for ( int i = 0 ; i < a.Length; i++)
{
Console .WriteLine (a[i]);
}
foreach ( string item in a)
{
Console .WriteLine (item);
}
}
}
}
二、二维数组的创建与遍历
方法一:
string[,] a=new string[5,3]
string[,] a=new string[5,3]{ {"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","i"},{"l","m","n"}};
方法二:
string[, ] a={ {"a","b","c"},{"d","e","f"},{"g","h","i"},{"j","k","i"},{"l","m","n"}};
二维数组的遍历:for和foreach
知识点: GetUpperBound(int variable>维度数字<);获取指定维度的上限
GetLowerBound( int variable>维度数字<);获取指定维度的下限
using System;
namespace practice3
{
class MainClass
{
public static void Main( string [] args)
{
string [,] a = new string [,]
{ { "张三" , "男" , "18" }, { "李四" , "女" , "19" }, { "王五" , "男" , "18" } };
for ( int i = 0 ; i < a.GetUpperBound ( 0 )+ 1 ; i++)
{
for ( int j = 0 ; j < a.GetUpperBound ( 1 )+ 1 ; j++)
{
Console .Write(a[i,j]+ " \t " );
}
Console .WriteLine ();
}
}
}
}