123
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Arrays { class Program { static void Main(string[] args) { } void setSimpleArrays(){ //way1 int[] myInts = new int[3]; myInts[0]=0; myInts[1]=1; myInts[2]=2; //way2 int[] myInts2 = new int[] { 1, 2, 3 }; //way3 int[] myInts3 = new int[3] { 1, 2, 3 }; //way4 int[] myInts4 = { 1, 2, 3 }; string[] myStrings = new string[3]; myStrings[0] = "1"; myStrings[1] = "2"; myStrings[2] = "3"; } //set var arrays void setVarArrays() { var a = new[] { 1, 2, 3 }; } //array of one type objs object[] objArr = new object[] { "obj1", "obj2", "obj3" }; //array of different type objs Shape[] shapes = { new Triangle("Triangle"), new Hexagon("Hexagon"), new Circle("Circle") }; //multi arrays int[,] matrix = new int[7, 7]; void fillMatrix() { for (var i = 0; i < 7; i++) { for (var j = 0; j < 7; j++) { matrix[i,j]= i * j; } } } // jagged Arrays private static void jaggedArrays() { int[][] myJaggedArray = new int[5][]; for (var i = 0; i < myJaggedArray.Length; i++) { myJaggedArray[i] = new int[i + 7]; } for (int i = 0; i < 5; i++) { for (int j = 0; j < myJaggedArray[i].Length; j++) { Console.WriteLine(myJaggedArray[i][j]+" "); // Console.WriteLine(); } // Console.WriteLine(); } } //System.Array private static void SystemArrayExamples() { var a = new[] {1,2,3}; System.Array.Reverse(a); System.Array.Sort(a); var b = new[] {0}; a.CopyTo(b,1); } } } |