数组是对相同类型的一组数据的封装。数组定义的时候,要说明是对哪一种类型的封装,并且要指定长度。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace TestArrayList 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 //System.Array 13 //1、数组[]特定类型、固定长度 14 string[] str1 = new string[3]; 15 str1[0] = "a"; 16 str1[1] = "b"; 17 str1[2] = "c"; 18 Console.WriteLine(str1[2]); 19 20 string[] str2 = new string[] { "a", "b", "c" }; 21 Console.WriteLine(str2[0]); 22 23 string[] str3 = { "a", "b", "c" }; 24 Console.WriteLine(str3[0]); 25 26 //2.二维数组 27 //int[,] intArray = new int 28 int[,] intArray = new int[2, 3]; 29 intArray[0, 0] = 1; 30 intArray[0, 1] = 11; 31 intArray[0, 2] = 111; 32 intArray[1, 0] = 2; 33 intArray[1, 1] = 22; 34 intArray[1, 2] = 222; 35 Console.WriteLine("{0},{1},{2}", intArray[0, 0], intArray[0, 1], intArray[0, 2]); 36 Console.WriteLine("{0},{1},{2}", intArray[1, 0], intArray[1, 1], intArray[1, 2]); 37 38 //3多维数组 39 int[, ,] intArray1 = new int[,,] 40 { 41 { { 1,1},{ 11,11},{ 111,111}}, 42 { { 2,2},{ 22,22},{ 33,33}}, 43 { { 3,3},{ 33,33},{ 333,333}} 44 }; 45 Console.WriteLine("多维数组"); 46 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]); 47 Console.WriteLine("{0},{1},{2},{3}", intArray1[1, 0, 0], intArray1[1, 0, 1], intArray1[1, 1, 0], intArray1[1, 1, 1]); 48 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]); 49 50 //4交错数组即数组的数组 51 int[][] intArray2 = new int[4][]; 52 intArray2[0] = new int[] { 1 }; 53 intArray2[1] = new int[] { 2, 22 }; 54 intArray2[2] = new int[] { 3, 33, 333 }; 55 intArray2[3] = new int[] { 4, 44, 444, 4444 }; 56 Console.WriteLine("交错数组"); 57 for (int i = 0; i < intArray2.Length; i++) 58 { 59 for (int j = 0; j < intArray2[i].Length; j++) 60 { 61 Console.WriteLine("{0}", intArray2[i][j]); 62 } 63 } 64 Console.ReadKey(); 65 int[] myIntArray = new int[5] { 1, 2, 3, 4, 5 }; 66 Object[] myObjArray = new Object[5] { 26, 27, 28, 29, 30 }; 67 Console.WriteLine("Initially,"); 68 Console.Write("integer array:"); 69 PrintValues(myIntArray); 70 Console.Write("Object array: "); 71 PrintValues(myObjArray); 72 73 System.Array.Copy(myIntArray, myObjArray, 2); 74 75 Console.WriteLine("\n After copying the first two elements of the integer array to the Object array."); 76 Console.Write("integer array:"); 77 PrintValues(myIntArray); 78 Console.Write("Object array: "); 79 PrintValues(myObjArray); 80 81 System.Array.Copy(myObjArray, myObjArray.GetUpperBound(0) - 1, myIntArray, myIntArray.GetUpperBound(0) - 1, 2); 82 83 Console.WriteLine("\nAfter copying the last two elements of the object array to the integer array,"); 84 Console.Write("integer array:"); 85 PrintValues(myIntArray); 86 Console.Write("Object array:"); 87 PrintValues(myObjArray); 88 Console.ReadKey(); 89 } 90 91 public static void PrintValues(Object[] myArr) 92 { 93 foreach (Object i in myArr) 94 { 95 Console.Write("\t{0}", i); 96 } 97 Console.WriteLine(); 98 } 99 100 public static void PrintValues(int[] myArr)101 {102 foreach (int i in myArr)103 {104 Console.Write("\t{0}", i);105 }106 Console.WriteLine();107 }108 }109 }
运行结果如下:
数组是一种数据类型,并且二维数组在图像处理中会应用。一维数组的起始下标是[0]。二维数组的起始下标是[0,0]。交错也称参差数组的起始下标是[0][0]。
数组一定是固定长度和类型确定并且有序的,这种呆板的数据类型,导致它的INSERT,非常不方便,于是有了ArrayList
那么C#中数组是引用类型?还是值类型?C#中数组是引用类型,为什么是引用类型,依据是什么?