1.数组概念
数组是对相同类型的一组数据的封装。数组定义的时候,要说明是对哪一种类型的封装,并且要指定长度。
数组是一种数据类型,并且二维数组在图像处理中会应用。一维数组的起始下标是[0]。二维数组的起始下标是[0,0]。交错也称参差数组的起始下标是[0][0]。
数组一定是固定长度和类型确定并且有序的,这种呆板的数据类型,导致它的INSERT,非常不方便,于是有了ArrayList
2.示例代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShuZu
{
class Program
{
static void Main(string[] args)
{
//System.Array
//1、数组[]特定类型、固定长度
string[] str1 = new string[3];
str1[0] = "a";
str1[1] = "b";
str1[2] = "c";
Console.WriteLine(str1[2]);
string[] str2 = new string[] { "a", "b", "c" };
Console.WriteLine(str2[0]);
string[] str3 = { "a", "b", "c" };
Console.WriteLine(str3[0]);
//2.二维数组
//int[,] intArray = new int
int[,] intArray = new int[2, 3];
intArray[0, 0] = 1;
intArray[0, 1] = 11;
intArray[0, 2] = 111;
intArray[1, 0] = 2;
intArray[1, 1] = 22;
intArray[1, 2] = 222;
Console.WriteLine("{0},{1},{2}", intArray[0, 0], intArray[0, 1], intArray[0, 2]);
Console.WriteLine("{0},{1},{2}", intArray[1, 0], intArray[1, 1], intArray[1, 2]);
//3多维数组
int[,,] intArray1 = new int[,,]
{
{{1,1},{11,11},{111,111}},
{{2,2},{22,22},{33,33}},
{{3,3},{33,33},{333,333}}
};
Console.WriteLine("多维数组");
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[0, 0, 0], intArray1[0, 0, 1], intArray1[0, 1, 0], intArray1[0, 1, 1], intArray1[0, 2, 0], intArray1[0, 2, 1]);
Console.WriteLine("{0},{1},{2},{3}", intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1]);
Console.WriteLine("{0},{1},{2},{3},{4},{5}", intArray1[2, 0, 0], intArray1[2, 0, 1], intArray1[2, 1, 0], intArray1[2, 1, 1], intArray1[2, 2, 0], intArray1[2, 2, 1]);
//4交错数组即数组的数组
int[][] intArray2 = new int[4][];
intArray2[0] = new int[] { 1 };
intArray2[1] = new int[] { 2, 22 };
intArray2[2] = new int[] { 3, 33, 333 };
intArray2[3] = new int[] { 4, 44, 444, 4444 };
Console.WriteLine("交错数组");
for (int i = 0; i < intArray2.Length; i++)
{
for (int j = 0; j < intArray2[i].Length; j++)
{
Console.WriteLine("{0}", intArray2[i][j]);
}
}
Console.ReadKey();
int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 };
Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 };
Console.WriteLine("Initially,");
Console.Write("integer array:");
PrintValues(myIntArray);
Console.Write("Object array: ");
PrintValues(myObjArray);
System.Array.Copy(myIntArray, myObjArray, 2);
Console.WriteLine("\n After copying the first two elements of the integer array to the Object array.");
Console.Write("integer array:");
PrintValues(myIntArray);
Console.Write("Object array: ");
PrintValues(myObjArray);
System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2);
Console.WriteLine("\nAfter copying the last two elements of the object array to the integer array,");
Console.Write("integer array:");
PrintValues(myIntArray);
Console.Write("Object array:");
PrintValues(myObjArray);
Console.ReadKey();
}
public static void PrintValues(Object[] myArr)
{
foreach (Object i in myArr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine();
}
public static void PrintValues(int[] myArr)
{
foreach (int i in myArr)
{
Console.Write("\t{0}", i);
}
Console.WriteLine();
}
}
}
3.演示效果图