01 nums binary-long occurs 3 values 1, 2, 3. *> Can also do: set content of nums to (1, 2, 3) *> 5 is the size of the array 01 names string occurs 5. *> Can also do: 01 names string occurs any. set size of names to 5 set names(1) to "Rod" *> first element indexed as 1 set names(6) to "Jane" *> throws System.IndexOutOfRangeException *> COBOL cannot resize an array - use copy 01 names2 string occurs 7. invoke type Array::Copy(names, names2, names::Length) *> or: invoke names::CopyTo(names2, 0) *> or: invoke type Array::Resize(names, 7) 01 twoD float-short occurs any, any. set size of twoD to rows, cols 01 jagged binary-long occurs any, occurs any. set size of jagged to 3 set size of jagged(1) to 5 set jagged(1 5) to 5
int[] nums = {1, 2, 3}; for (int i = 0; i < nums.Length; i++) { Console.WriteLine(nums[i]); } // 5 is the size of the array string[] names = new string[5]; names[0] = "Rod"; names[5] = "Jane"; // Throws System.IndexOutOfRangeException // C# can't dynamically resize an array. Just copy into new array. string[] names2 = new string[7]; // or names.CopyTo(names2, 0); Array.Copy(names, names2, names.Length); float[,] twoD = new float[rows, cols]; twoD[2,0] = 4.5f; int[][] jagged = new int[3][] { new int[5], new int[2], new int[3] }; jagged[0][4] = 5;
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.