work! The first way is perhaps viewed as more as it clearly shows the type of array being “formal” declared again in the assignment. Assigning data items to an array after initialisation After we have initialised the array, we can int[] peopleAges = new int[5]; add data items, of that data type, into the array. peopleAges[0] = 31; peopleAges[1] = 24; All of the data items within an array peopleAges[2] = 12; must be of the same type. peopleAges[3] = 68; peopleAges[4] = 44; Here we have added 5 ages to our array. Remember that an array index starts at [0]! With a 2D array, we need to make sure string[,] namesAndAges = new string[3, 2]; that: namesAndAges[0, 0] = "Jill"; All the data items are of the same namesAndAges[0, 1] = "23"; type therefore ages here are – stored as strings namesAndAges[1, 0] = "Rashpal"; We remember the dimensions of namesAndAges[1, 1] = "43"; the array when assigning data! namesAndAges[2, 0] = "Leoung"; namesAndAges[2, 1] = "55"; The code on the left shows us that we Trying to assign a variable outside of the have tried to assign to [0,3] in the ‘23’ array boundary array. string[,] namesAndAges = new string[3, 2]; The are [3,2] and array’s bounds therefore [0,3] does not exist. It generates namesAndAges[0, 0] = "Jill"; the following run time error shown below. namesAndAges[0, 3] = "23"; These errors do not show in the IDE, and will simply result in the program crashing once executed as shown below. Array out of bounds run time error: Sorting arrays Sorting lists is usually useful and you can do this by using the Array.Sort() method. However, once sorted, the array will remain in a sorted state
work! The first way is perhaps viewed as more “formal” as it clearly shows the type of array being declared again in the assignment. Assigning data items to an array after initialisation int[] peopleAges = new int[5]; peopleAges[0] = 31; peopleAges[1] = 24; peopleAges[2] = 12; peopleAges[3] = 68; peopleAges[4] = 44; After we have initialised the array, we can add data items, of that data type, into the array. All of the data items within an array must be of the same type. Here we have added 5 ages to our array. Remember that an array index starts at [0]! string[,] namesAndAges = new string[3, 2]; namesAndAges[0, 0] = "Jill"; namesAndAges[0, 1] = "23"; namesAndAges[1, 0] = "Rashpal"; namesAndAges[1, 1] = "43"; namesAndAges[2, 0] = "Leoung"; namesAndAges[2, 1] = "55"; With a 2D array, we need to make sure that: All the data items are of the same type – therefore ages here are stored as strings We remember the dimensions of the array when assigning data! Trying to assign a variable outside of the array boundary string[,] namesAndAges = new string[3, 2]; namesAndAges[0, 0] = "Jill"; namesAndAges[0, 3] = "23"; The code on the left shows us that we have tried to assign ‘23’ to [0,3] in the array. The array’s bounds are [3,2] and therefore [0,3] does not exist. It generates the following run time error shown below. These errors do not show in the IDE, and will simply result in the program crashing once executed as shown below. Array out of bounds run time error: Sorting arrays Sorting lists is usually useful and you can do this by using the Array.Sort() method. However, once sorted, the array will remain in a sorted state
398694-programming-techniques-c-teacher-guide.docx