ArrayList和Array的区别
Array的容量是固定的,而ArrayList是可变的,
Array具有多维,而ArrayList是一维的
ArrayList提供添加,删除插入某一范围元素的方法而在Array中只能设置或获取一个元素的值
ArrayLlist是较为复杂的数组,它提供了一些Array未提供的功能
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Array
... {
class Program
...{
static void Main(string[] args)
...{ //使用ArrayList 前要引入命名空间System.Collections;
ArrayList arr = new ArrayList();
string str1;
while (true)
...{
Console.WriteLine("Add the word to ArrayList");
str1 = Console.ReadLine();
if (str1 =="end")
break;//只有输入"end" ,才会跳出while
arr.Add(str1);
for (int i = 0; i < arr.Count; i++)
Console.Write("{0} ", arr[i]);
Console.WriteLine();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Array
... {
class Program
...{
static void Main(string[] args)
...{ //使用ArrayList 前要引入命名空间System.Collections;
ArrayList arr = new ArrayList();
string str1;
while (true)
...{
Console.WriteLine("Add the word to ArrayList");
str1 = Console.ReadLine();
if (str1 =="end")
break;//只有输入"end" ,才会跳出while
arr.Add(str1);
for (int i = 0; i < arr.Count; i++)
Console.Write("{0} ", arr[i]);
Console.WriteLine();
}
}
}
}
多维数组:
int
[] arr1;
//
定义整形一维数组
int [,] arr2; // 定义整形二维数组
int [,] arr3 = new int [ 2 , 3 ];
int [,] arr3 = new int [ 2 , 3 ] ... { ...{ 1, 3, 2 }, ...{ 4, 5, 6 } } ;
int [][] j2; // 不规则数组,也称为数组的数组
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,3,2,4,5,6} ;
int [,] arr2; // 定义整形二维数组
int [,] arr3 = new int [ 2 , 3 ];
int [,] arr3 = new int [ 2 , 3 ] ... { ...{ 1, 3, 2 }, ...{ 4, 5, 6 } } ;
int [][] j2; // 不规则数组,也称为数组的数组
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,3,2,4,5,6} ;
static
void
Main(
string
[] args)
... {
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();
}
Console.ReadLine();
}
... {
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();
}
Console.ReadLine();
}