1.
(1)Array的容量是固定的,而ArrayList的容量可根据需要自动扩充。
(2)ArrayList提供添加、插入或者一处某一范围元素的方法。在Array中,你只能一次获取或设置一个元素的值。
(3)Array可以具有多个维度,ArrayList始终只是一维的。
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);
Console.WriteLine();
for (int i = 0; i < arr.Count;i++ )
{
Console.WriteLine("{0}",arr[i]);
}
}
}
}
2.多维数组
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];
数组的数组
int[] [] j2;//不规则数组:数组的数组
int[] [] j2 =new int[3][];
j2[0]=new int[] {1,2,3};
j2[1]=new int[] {1,2,3,4};
j2 [2]=new int[] {1,2,3,4,5};
int[] [] [] j3;//数组的数组的数组
例子:
using System;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}\t",arr[i,j]);
}
Console.WriteLine();
}
}
}