您的位置:首页 > 编程语言 > C#

陈广 C#学习笔记(数组2-3)

2010-04-14 22:37 459 查看

C#视频(陈广版)学习笔记2-3

本2.3节讲解C#中的数组

using System;
class Test
{
static void Main() //声明一个Main()入口函数
{
int[] arr=new int[] {1,2,3}; //数组的声明用到new关键字,后面跟着数组的类型,然

后可以直接给它赋值,这个数组有三个元素,1,2,3
//下面用一个循环打印出来
for (int i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]); //用下标的方法来访问数组
}
}

数组,对于局部变量和域声明,允许一种简写形式,这样就不需要去声明数组类型.
例如,int[] arr=new int[]{1,2,3}; 可以简写为int[] arr={1,2,3};

using System;
class Test
{
static void Main() //声明一个Main()入口函数
{
//int[] arr=new int[] {1,2,3};
int [] arr={1,2,3};

for (int i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]);
}
}

输出效果是一样的,当然也可以先声明数组长度再赋值

using System;
class Test
{
static void Main() //声明一个Main()入口函数
{
int[] arr=new int[3];
arr[0]=1;
arr[1]=2;
arr[2]=3;

for (int i=0;i<arr.Length;i++)
Console.WriteLine(arr[i]);
}
}

效果一样,当然也可以用一个循环来给数组赋值

using System;
class Test
{
static void Main()
{
int[] arr=new int[3];
arr[0]=1;
arr[1]=2;
arr[2]=3;
//下面用一个循环打印出来
foreach (int i in arr) //foreach方法访问数组,声明一个i来读取数组里面的内容
Console.WriteLine(i);
}
}

===

一般情况下,数组的长度是未知的,且是动态改变的,也就是通常说的动态数组,但C#并不支持动态数组,可

以用下面将讲到的arrayList来实现.

但是可以在数组声明时动态指定数组长度,这就要借变量来声明数组的长度.
演示如下:

using System;
class Test
{
static void PrintArr(int ArrLength) //Main()函数只能调用域里面的静态成员,所以声明成

satic
{
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]); //打印,将i值赋予0位

置,arr[i]值赋予1位置

}
static void Main()
{
int i=1;
while (i>0) //输入小于等于0时,结束打印
{
Console.WriteLine("Please enter array's length:");
i=Int32.Parse(Console.ReadLine()); //readline返回的是字符,所以要用

int32.parse函数转换为整数再赋给i
PrintArr(i); //把i作为参数传递到PrintArr
}

}
}

static void PrintArr(int ArrLength) //Main()函数只能调用域里面的静态成员,所以声明成satic
如果不想声明为static,可以在声明在另一个类里面.如,

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]); //打印,将i值赋予0位

置,arr[i]值赋予1位置

}
}
class Test
{
static void Main()
{
SetArray a=new SetArray();

int i=1;
while (i>0) //输入小于等于0时,结束打印
{
Console.WriteLine("Please enter array's length:");
i=Int32.Parse(Console.ReadLine()); //readline返回的是字符,所以要用

int32.parse函数转换为整数再赋给i
a.PrintArr(i); //把i作为参数传递到PrintArr
}

}
}

===3====

C#并不支持动态数组.要使用到动态数组,可以用ArrayList来代替

<1.ArrayList位于System.Collections命名空间
<2.ArrayList对象是较为复杂的数组.ArrayList类提供了Array类未提供的一些功能

ArrayList与Array的区别:

<1.Array的容量是固定的,而ArrayList的容量可根据需要自动扩充.
<2.ArrayList提供添加,插入或移除某一范围元素的方法.在Array中,只能一次获取或设置一个元素的值.

例:
using System;
using System.collections;
class ArrList
{
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); //演示了如何给ArrayList添加一个元素
Console.WriteLine(); //打印空行
for (int i=0;i<arr.Count;i++)
Console.Write("{0} ",arr[i]);
Console.WriteLine("/n"); //打印换行符
}
}
}

详细使用见MSDN

多维数组:

int[] a1; //整数类型的一维数组
int[,] a2; //整数类型的二维数组
int[,] a2=new int[2,3];
int[,] a2=new int[,]{{1,2,3},{4,5,6}};

int[,,] a3; //整数类型的三维数组
int[,,] a3=new int[10,20,30]; //定义三维数组,空间为10X20X30

数组的数组:
int[][] j2; //类似C中的二维数组,C#中是不规划数组:数组的数组

int[][] j2=new int[3][];
j2[0]=new int[]{1,2,3};
j2[1]=new int[]{1,2,3,4,5,6};
j2[2]=new int[]{1,2,3,4,5,6,7,8};

int[][][] j3; //数组的数组的数组

一个数组演示二维矩阵:

using system;
class Matrix
{
static void Main()
{
int[,] a2 = new int[, ] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] ij = new int[4, 6];
int m;
//int k = ij.Rank; //获得数组的维数
for (int i = 0; i < ij.GetLength(0); i++)
{
for (int j = 0; j < ij.GetLength(1); j++)
{
m = (i + 1) * 10 + (j + 1);
Console.Write("{0} ", m);
}
Console.WriteLine();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: