数组浅谈

数组是引用类型,数组和集合都包含了同一个类型的多个对象(如果需要包含不同类型的多个对象需要使用元祖类型)

数组在指定了大小后就不可用在改变

也可以自定义数组类型

////class Array
////{
//// static void Main()
//// {
//// int[] a1 = new int[] { 1,2,3 };
//// int[,] a2 = new int[,] { { 1 }, { 4 } };
//// int[,,] a3 = new int[, , ]{{{1},{1},{1}},{{5},{5},{5} }};
//// int[,,] a4=new int[,,] { { { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } }, { { 1, 2, 3 },{ 4, 5, 6 },{ 7, 8, 9 } } };
//// 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, 9 };
//// }

////}
////class test
////{
//// static void Main()
//// {
//// int[] arr = new int[5];
//// for (int i = 0; i < arr.Length; i++) {
//// arr[i] = i + i;
//// }
//// for (int i = 0; i < arr.Length; i++) {
//// Console.WriteLine("arr[{0}]={1}",i,arr[i]);

//// }
//// Console.Read();
//// }

////}
//public class Person
//{
// public string FirstName { get; set; }
// public string LastName { get; set; }
// public override string ToString()
// {
// return String.Format("{0},{1}", FirstName, LastName);
// }

// Person[] myPersons2 =
// {
//new Person { FirstName="Ayrton", LastName="Senna"},
//new Person { FirstName="Michael", LastName="Schumacher"}
//};

//}

//class test
// {
// static void Main()
// {
// Person[] myPersons = new Person[2];
// myPersons[0] = new Person { FirstName = "Ayrton", LastName = "Senna" };
// myPersons[1] = new Person { FirstName = "Michael", LastName = "Schumacher" };
// }

// }

锯齿数组:

就是一个数组里面还有数组

 

class JaggedArray
{
//public JaggedArray(){
// int[][] a = new int[3][];
// a[0] = new int[3];
// }
static void Main() {
int[][] a = new int[3][];
a[0] = new int[3];
a[1] = new int[4];
int[][] b = new int[2][] { new int[] { 1,2, 3 }, new int[] { 4, 5, 6 } };
int[][] c = { new int[3] { 1, 2, 3 },new int[4] { 4,5,6,7} };
for (int i = 0; i < b.Length; i++)
{
for (int j = 0; j < b[i].Length; j++)
{
Console.WriteLine(b[i][j]);
}
}
Console.WriteLine("-----------");
foreach (int []i in c)
{
foreach(int j in i)
{
Console.WriteLine(j);
}
}
Console.Read();

}

}

使用双层循环来迭代数组,第一层用来循环数组的每一行,第二层用来循环数组里面的数

详见:https://www.cnblogs.com/afei-24/p/6738128.html

posted @ 2018-08-30 14:06  小矮子的小胖子  阅读(149)  评论(0编辑  收藏  举报