2. Pascal case AddUp(..) Camel case firstNumber C Programming Language case first_number a word on naming conventions
3. Why use Arrays? How to set up Array? Arrays and Loops Set size of Arrays at runtime Foreach loop What will we learn?
4. The variables we have been working with so far have only been able to hold one value at a time Example: int lotteryNumber1 = 1; int lotteryNumber2 = 2; int lotteryNumber3 = 3; : An Array allows you to use just one identifying name that refers to lots of values Why use Arrays?
5. 1. Declaration: int[] lotteryNumbers; float[] myFloatValues; string[] myStrings; 2. Size of array: lotteryNumbers = new int[4]; myFloatValues = new float[10]; myStrings = new string[5]; How to setup an Array
6. Declaration and setting the size in one line int[] lotteryNumbers = new int[4]; float[] myFloatValues = new float[10]; string[] myStrings = new string[5];
7. arrayName[position] = arrayValue; int[] lotteryNumbers = new int[4]; lotteryNumbers[0] = 1; // first array lotteryNumbers[1] = 2; lotteryNumbers[2] = 3; lotteryNumbers[3] = 4; // last (4th) array Assigning values
8. int[] lotteryNumbers = new int[4]; lotteryNumbers[0] = 1; lotteryNumbers[1] = 2; lotteryNumbers[2] = 3; lotteryNumbers[3] = 4; First index is ZERO
9. int[] lotteryNumbers = new int[4]; lotteryNumbers[0] = 1; lotteryNumbers[1] = 2; lotteryNumbers[2] = 3; lotteryNumbers[3] = 4; Last index is (SIZE -1)
10. int[] lotteryNumbers = new int[4]; lotteryNumbers[0] = 1; lotteryNumbers[1] = 2; lotteryNumbers[2] = 3; lotteryNumbers[3] = 4; Total number of array = SIZE
11. int[] lotteryNumbers = new int[4] {1, 2, 3, 4}; Declare, Set Array Size and Assign Values in one line Declare
14. To loop through the following array: lotteryNumbers[0] lotteryNumbers[1] lotteryNumbers[2] lotteryNumbers[3] for (inti = 0; i != lotteryNumbers.Length; i++) { lotteryNumber[i] ….. // not complete } Part 2 Arrays and Loops
15. for (inti = 0; i != lotteryNumbers.Length; i++) { lotteryNumber[i] ….. // not complete } starts from 0 The first index is ZERO
16. for (inti = 0; i != lotteryNumbers.Length; i++) { lotteryNumber[i] ….. // not complete } Length is equal to the SIZE of array The last index should be (Length – 1)
17. for (inti = 0; i != lotteryNumbers.Length; i++) { lotteryNumber[i] ….. // not complete } i < lotteryNumers.Length
18. for (inti = 0; i != lotteryNumbers.Length; i++) { lotteryNumber[i] ….. // not complete } i <= lotteryNumers.Length -1
19. New Solution and project: SpfChapter8 Save all Change the project name to "Part 2 Arrays and Loops" Add a button and a listBox Add codes into the button click method: Hands On
27. Looping through from 2nd Array to last Array int() lotteryNumbers = int(5); for (inti=2; i != lotteryNumbers.Length; ++i) { lotteryNumbers(i) = 0; } Spots the Errors
28. Why use Arrays? How to set up Array? Arrays and Loops Review
29. Set size of Arrays at runtime Foreach loop What will we learn?
30. The size of an array refers to how many items it holds But sometimes, you just don’t know how big the array needs to be – for example, when the application depends on user’s input during runtime Part 3 Set the Size of a C# array at RunTime
33. The size of the array is set only during runtime
34. Compare for loop and foreach loop: for (int i = 0; i != arraySize.Length; i++) foreach (int number in arraySize) foreach loop
35. Compare for loop and foreach loop: for (int i = 0; i != arraySize.Length; i++) foreach (int number in arraySize) foreach loop loop through from 0 to (Length-1) counter
36. Compare for loop and foreach loop: for (int i = 0; i != arraySize.Length; i++) foreach (int number in arraySize) foreach loop loop through from 0 to (Length-1) counter individual element in array
37. Compare for loop and foreach loop: for (int i = 0; i != arraySize.Length; i++) foreach (int number in arraySize) foreach loop Individual element: arraySize[i] Individual element: number
39. string Array is similar to integer Array string[] arrayStrings; arrayStrings = new string[5]; foreach loop for string foreach (string arrayElement in arrayStrings) Using foreach with string Array