KEMBAR78
C# Arrays | PPTX
Chapter 8C# .NET Arrays
Pascal caseAddUp(..)Camel casefirstNumberC Programming Language casefirst_numbera word on naming conventions
Why use Arrays?How to set up Array?Arrays and LoopsSet size of Arrays at runtimeForeach loopWhat will we learn?
The variables we have been working with so far have only been able to hold one value at a timeExample:int lotteryNumber1 = 1;int lotteryNumber2 = 2;int lotteryNumber3 = 3;   :An Array allows you to use just one identifying name that refers to lots of valuesWhy use Arrays?
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
Declaration and setting the size in one lineint[] lotteryNumbers = new int[4];float[] myFloatValues = new float[10];string[] myStrings = new string[5];
arrayName[position] = arrayValue;int[] lotteryNumbers = new int[4];lotteryNumbers[0] = 1;       // first arraylotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4;   // last (4th) arrayAssigning values
int[] lotteryNumbers = new int[4];lotteryNumbers[0] = 1;   lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; First index is ZERO
int[] lotteryNumbers = new int[4];lotteryNumbers[0] = 1;   lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; Last index is (SIZE -1)
int[] lotteryNumbers = new int[4];lotteryNumbers[0] = 1;   lotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; Total number of array = SIZE
int[] lotteryNumbers = new int[4] {1, 2, 3, 4};Declare, Set Array Size and Assign Values in one lineDeclare
int[] lotteryNumbers = new int[4] {1, 2, 3, 4};Set Size of Array
int[] lotteryNumbers = new int[4] {1, 2, 3, 4};Assign values
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
for (inti = 0; i != lotteryNumbers.Length; i++){lotteryNumber[i]     ….. // not complete}starts from 0The first index is ZERO
for (inti = 0; i != lotteryNumbers.Length; i++){lotteryNumber[i]     ….. // not complete}Length is equal to the SIZE of arrayThe last index should be (Length – 1)
for (inti = 0; i != lotteryNumbers.Length; i++){lotteryNumber[i]     ….. // not complete}i < lotteryNumers.Length
for (inti = 0; i != lotteryNumbers.Length; i++){lotteryNumber[i]     ….. // not complete}i <= lotteryNumers.Length -1
New Solution and project:  SpfChapter8Save allChange the project name to "Part 2 Arrays and Loops"Add a button and a listBoxAdd codes into the button click method:Hands On
Use Loop
Use Loop
4
change the size of Array to 49:
Looping through from 2nd Array to last Arrayint() lotteryNumbers = int(5);for (inti=2; i != lotteryNumbers.Length; ++i){lotteryNumbers(i) = 0;} Spots the Errors
Why use Arrays?How to set up Array?Arrays and LoopsReview
Set size of Arrays at runtimeForeach loopWhat will we learn?
The size of an array refers to how many items it holdsBut sometimes, you just don’t know how big the array needs to be – for example, when the application depends on user’s input during runtimePart 3 Set the Size of a C# array at RunTime
Add another button and textboxContinue from previous project
Add codes for button2 Click method:
The size of the array is set only during runtime
Compare for loop and foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loop
Compare for loop and foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counter
Compare for loop and foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counterindividual element in array
Compare for loop and foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loopIndividual element: arraySize[i]Individual element: number
Continue from previous project (Extra)
string Array is similar to integer Array    string[] arrayStrings;arrayStrings = new string[5];foreach loop for stringforeach (string arrayElement in arrayStrings)Using foreach with string Array
Continue from previous project
string array using for loop
string array using foreach

C# Arrays

  • 1.
  • 2.
    Pascal caseAddUp(..)Camel casefirstNumberCProgramming Language casefirst_numbera word on naming conventions
  • 3.
    Why use Arrays?Howto set up Array?Arrays and LoopsSet size of Arrays at runtimeForeach loopWhat will we learn?
  • 4.
    The variables wehave been working with so far have only been able to hold one value at a timeExample:int lotteryNumber1 = 1;int lotteryNumber2 = 2;int lotteryNumber3 = 3; :An Array allows you to use just one identifying name that refers to lots of valuesWhy 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 settingthe size in one lineint[] 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 arraylotteryNumbers[1] = 2;lotteryNumbers[2] = 3;lotteryNumbers[3] = 4; // last (4th) arrayAssigning 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 lineDeclare
  • 12.
    int[] lotteryNumbers =new int[4] {1, 2, 3, 4};Set Size of Array
  • 13.
    int[] lotteryNumbers =new int[4] {1, 2, 3, 4};Assign values
  • 14.
    To loop throughthe 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 0The first index is ZERO
  • 16.
    for (inti =0; i != lotteryNumbers.Length; i++){lotteryNumber[i] ….. // not complete}Length is equal to the SIZE of arrayThe 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 andproject: SpfChapter8Save allChange the project name to "Part 2 Arrays and Loops"Add a button and a listBoxAdd codes into the button click method:Hands On
  • 20.
  • 22.
  • 25.
  • 26.
    change the sizeof Array to 49:
  • 27.
    Looping through from2nd Array to last Arrayint() lotteryNumbers = int(5);for (inti=2; i != lotteryNumbers.Length; ++i){lotteryNumbers(i) = 0;} Spots the Errors
  • 28.
    Why use Arrays?Howto set up Array?Arrays and LoopsReview
  • 29.
    Set size ofArrays at runtimeForeach loopWhat will we learn?
  • 30.
    The size ofan array refers to how many items it holdsBut sometimes, you just don’t know how big the array needs to be – for example, when the application depends on user’s input during runtimePart 3 Set the Size of a C# array at RunTime
  • 31.
    Add another buttonand textboxContinue from previous project
  • 32.
    Add codes forbutton2 Click method:
  • 33.
    The size ofthe array is set only during runtime
  • 34.
    Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loop
  • 35.
    Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counter
  • 36.
    Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach looploop through from 0 to (Length-1) counterindividual element in array
  • 37.
    Compare for loopand foreach loop:for (int i = 0; i != arraySize.Length; i++)foreach (int number in arraySize)foreach loopIndividual element: arraySize[i]Individual element: number
  • 38.
    Continue from previousproject (Extra)
  • 39.
    string Array issimilar to integer Array string[] arrayStrings;arrayStrings = new string[5];foreach loop for stringforeach (string arrayElement in arrayStrings)Using foreach with string Array
  • 40.
  • 41.
  • 43.