SlideShare uma empresa Scribd logo
1 de 40
Microsoft Visual C# 2010
       Fourth Edition



        Chapter 6
       Using Arrays
Objectives
• Declare an array and assign values to array
  elements
• Access array elements
• Search an array using a loop
• Use the BinarySearch(), Sort(), and
  Reverse() methods
• Use multidimensional arrays
• Learn about array issues in GUI programs



Microsoft Visual C# 2010, Fourth Edition         2
Declaring an Array and Assigning
         Values to Array Elements
• Array
    – List of data items that all have the same data type and
      the same name
    – Each item is distinguished from the others by an index
• Declaring and creating an array
        double[] sales;
        sales = new double[20];
• new operator
    – Used to create objects
• You can change the size of an array

Microsoft Visual C# 2010, Fourth Edition                   3
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)
• Array element
    – Each object in an array
• Subscript (or index)
    – Integer contained within square brackets that indicates
      the position of one of an array’s elements
    – Array’s elements are numbered beginning with 0
• “Off by one” error
    – Occurs when you forget that the first element in an
      array is element 0


Microsoft Visual C# 2010, Fourth Edition                    4
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   5
Declaring an Array and Assigning
      Values to Array Elements (cont'd.)
• Assigning a value to an array element
          sales[0] = 2100.00;
• Printing an element value
          Console.WriteLine(sales[19]);




Microsoft Visual C# 2010, Fourth Edition   6
Initializing an Array

• In C#, arrays are objects
    – Arrays are instances of a class named
      System.Array
• Initializing objects
    – Numeric fields: 0
    – Character fields: ‘u0000’ or null
    – bool fields: false
• Initializer list
    – List of values provided for an array


Microsoft Visual C# 2010, Fourth Edition         7
Initializing an Array (cont'd.)

• Initializer list examples
    int[] myScores = new int[5] {100, 76, 88,
                     100, 90};
    int[] myScores = new int[] {100, 76, 88,
                     100, 90};
    int[] myScores = {100, 76, 88, 100, 90};




Microsoft Visual C# 2010, Fourth Edition        8
Accessing Array Elements

• The power of arrays becomes apparent when you
  use subscripts
    – Can be variables rather than constant values
• Using a loop to perform arithmetic on each element
        for (int sub = 0; sub < 5; ++sub)
          myScores[sub] += 3;




Microsoft Visual C# 2010, Fourth Edition               9
Using the Length Property

• Length property
    – Member of the System.Array class
    – Automatically holds an array’s length
• Examples
        int[] myScores = {100, 76, 88, 100, 90};
        Console.WriteLine(“Array size is {0}”,
                          myScores.Length);
        for (int x = 0; x < myScores.Length; ++x)
        Console.WriteLine(myScores[x]);



Microsoft Visual C# 2010, Fourth Edition            10
Using foreach

• foreach statement
    – Cycles through every array element without using a
      subscript
    – Uses a temporary iteration variable
          • Automatically holds each array value in turn
• Example
    double[] payRate = {6.00, 7.35, 8.12, 12.45, 22.22};
    foreach(double money in payRate)
    Console.WriteLine(“{0}”, money.ToString(“C”));



Microsoft Visual C# 2010, Fourth Edition                   11
Using foreach (cont'd.)

• Used under the following circumstances
    – When you want to access every array element
    – Since the iteration variable is read-only
          • You cannot assign a value to it




Microsoft Visual C# 2010, Fourth Edition            12
Using foreach with Enumerations




Microsoft Visual C# 2010, Fourth Edition   13
Searching an Array Using a Loop

• Searching options
    – Using a for loop
    – Using a while loop




Microsoft Visual C# 2010, Fourth Edition   14
Using a for Loop to Search an Array

• Use a for statement to loop through the array
    – Set a Boolean variable to true when a match is found
• Solution is valid even with parallel arrays




Microsoft Visual C# 2010, Fourth Edition               15
Using a for Loop to Search an Array
              (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   16
Using a while Loop to Search an
                  Array
• Use a while loop to search for a match




Microsoft Visual C# 2010, Fourth Edition   17
Microsoft Visual C# 2010, Fourth Edition   18
Searching an Array for a Range Match
• Range match
    – Determines the pair of limiting values between which a
      value falls




Microsoft Visual C# 2010, Fourth Edition                 19
Searching an Array for a Range Match
              (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   20
Using the BinarySearch(),
     Sort(),and Reverse() Methods
• System.Array class contains a variety of useful,
  built-in methods that can search, sort, and
  manipulate array elements




Microsoft Visual C# 2010, Fourth Edition             21
Using the BinarySearch() Method

• BinarySearch() method
    – Finds a requested value in a sorted array
    – Member of the System.Array class
• Do not use BinarySearch() under these
  circumstances
    – If your array items are not arranged in ascending order
    – If your array holds duplicate values and you want to
      find all of them
    – If you want to find a range match rather than an exact
      match

Microsoft Visual C# 2010, Fourth Edition                  22
Using the BinarySearch() Method
               (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   23
Using the Sort() Method

• Sort() method
    – Arranges array items in ascending order
    – Use it by passing the array name to Array.Sort()




Microsoft Visual C# 2010, Fourth Edition             24
Using the Sort() Method (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   25
Using the Reverse() Method

• Reverse() method
    – Reverses the order of items in an array
    – Element that starts in position 0 is relocated to position
      Length – 1
    – Use it by passing the array name to the method




Microsoft Visual C# 2010, Fourth Edition                     26
Using the Reverse() Method
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   27
Using Multidimensional Arrays

• One-dimensional or single-dimensional array
    – Picture as a column of values
    – Elements can be accessed using a single subscript
• Multidimensional arrays
    – Require multiple subscripts to access the array
      elements
    – Two-dimensional arrays
          • Have two or more columns of values for each row
          • Also called rectangular array, matrix, or a table


Microsoft Visual C# 2010, Fourth Edition                        28
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   29
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   30
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   31
Using Multidimensional Arrays
                     (cont'd.)
• Three-dimensional array




Microsoft Visual C# 2010, Fourth Edition   32
Using Multidimensional Arrays
                     (cont'd.)
• Jagged array
    – One-dimensional array in which each element is
      another one-dimensional array
    – Each row can be a different length




Microsoft Visual C# 2010, Fourth Edition               33
Using Multidimensional Arrays
                     (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   34
Array Issues in GUI Programs

• If array values change based on user input
   – Array must be stored outside any method that reacts to
     the user’s event




 Microsoft Visual C# 2010, Fourth Edition                35
Array Issues in GUI Programs (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   36
You Do It

• Activities to explore
   – Creating and Using an Array
   – Using the Sort() and Reverse() Methods




 Microsoft Visual C# 2010, Fourth Edition         37
Summary
• An array is a list of data items
     – All of which have the same type and the same name
     – Items are distinguished using a subscript or index
• In C#, arrays are objects of a class named
  System.Array
• The power of arrays becomes apparent when you
  begin to use subscripts
• Subscript you use remains in the range of 0
  through length -1


Microsoft Visual C# 2010, Fourth Edition                38
Summary (cont'd.)

• foreach statement cycles through every array
  element without using subscripts
• You can compare a variable to a list of values in an
  array
• You can create parallel arrays to more easily
  perform a range match
• The BinarySearch() method finds a requested
  value in a sorted array
• The Sort() method arranges array items in
  ascending order

Microsoft Visual C# 2010, Fourth Edition             39
Summary (cont'd.)

• The Reverse() method reverses the order of
  items in an array
• Multidimensional arrays require multiple subscripts
  to access the array elements
• Types of multidimensional arrays
     – Two-dimensional arrays (rectangular arrays)
     – Three-dimensional arrays
     – Jagged arrays



Microsoft Visual C# 2010, Fourth Edition             40

Mais conteúdo relacionado

Mais procurados

Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
Terry Yoast
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
swatisinghal
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
Terry Yoast
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
Terry Yoast
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
Terry Yoast
 

Mais procurados (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Chap02
Chap02Chap02
Chap02
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
Chapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of JavaChapter 2 - Basic Elements of Java
Chapter 2 - Basic Elements of Java
 
9781439035665 ppt ch02
9781439035665 ppt ch029781439035665 ppt ch02
9781439035665 ppt ch02
 
Course Breakup Plan- C
Course Breakup Plan- CCourse Breakup Plan- C
Course Breakup Plan- C
 
Intake 38_1
Intake 38_1Intake 38_1
Intake 38_1
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
9781439035665 ppt ch04
9781439035665 ppt ch049781439035665 ppt ch04
9781439035665 ppt ch04
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
9781111530532 ppt ch03
9781111530532 ppt ch039781111530532 ppt ch03
9781111530532 ppt ch03
 
Templates c++ - prashant odhavani - 160920107003
Templates   c++ - prashant odhavani - 160920107003Templates   c++ - prashant odhavani - 160920107003
Templates c++ - prashant odhavani - 160920107003
 
9781111530532 ppt ch04
9781111530532 ppt ch049781111530532 ppt ch04
9781111530532 ppt ch04
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
 
Chapter2
Chapter2Chapter2
Chapter2
 

Destaque (12)

Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
C
CC
C
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
5 arrays
5   arrays5   arrays
5 arrays
 
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick LearningOracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Semelhante a Csc153 chapter 06

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 

Semelhante a Csc153 chapter 06 (20)

Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
lecture_3.ppt
lecture_3.pptlecture_3.ppt
lecture_3.ppt
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python for Data Analysis.pdf
Python for Data Analysis.pdfPython for Data Analysis.pdf
Python for Data Analysis.pdf
 
Python-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptxPython-for-Data-Analysis.pptx
Python-for-Data-Analysis.pptx
 
Python-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdfPython-for-Data-Analysis.pdf
Python-for-Data-Analysis.pdf
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
Python for data analysis
Python for data analysisPython for data analysis
Python for data analysis
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
Arrays.pptx
 Arrays.pptx Arrays.pptx
Arrays.pptx
 
Net framework
Net frameworkNet framework
Net framework
 
Numpy.pdf
Numpy.pdfNumpy.pdf
Numpy.pdf
 
9781285852744 ppt ch08
9781285852744 ppt ch089781285852744 ppt ch08
9781285852744 ppt ch08
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
 
0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf0-Slot18-19-20-ContiguousStorage.pdf
0-Slot18-19-20-ContiguousStorage.pdf
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Último (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Csc153 chapter 06

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 6 Using Arrays
  • 2. Objectives • Declare an array and assign values to array elements • Access array elements • Search an array using a loop • Use the BinarySearch(), Sort(), and Reverse() methods • Use multidimensional arrays • Learn about array issues in GUI programs Microsoft Visual C# 2010, Fourth Edition 2
  • 3. Declaring an Array and Assigning Values to Array Elements • Array – List of data items that all have the same data type and the same name – Each item is distinguished from the others by an index • Declaring and creating an array double[] sales; sales = new double[20]; • new operator – Used to create objects • You can change the size of an array Microsoft Visual C# 2010, Fourth Edition 3
  • 4. Declaring an Array and Assigning Values to Array Elements (cont'd.) • Array element – Each object in an array • Subscript (or index) – Integer contained within square brackets that indicates the position of one of an array’s elements – Array’s elements are numbered beginning with 0 • “Off by one” error – Occurs when you forget that the first element in an array is element 0 Microsoft Visual C# 2010, Fourth Edition 4
  • 5. Declaring an Array and Assigning Values to Array Elements (cont'd.) Microsoft Visual C# 2010, Fourth Edition 5
  • 6. Declaring an Array and Assigning Values to Array Elements (cont'd.) • Assigning a value to an array element sales[0] = 2100.00; • Printing an element value Console.WriteLine(sales[19]); Microsoft Visual C# 2010, Fourth Edition 6
  • 7. Initializing an Array • In C#, arrays are objects – Arrays are instances of a class named System.Array • Initializing objects – Numeric fields: 0 – Character fields: ‘u0000’ or null – bool fields: false • Initializer list – List of values provided for an array Microsoft Visual C# 2010, Fourth Edition 7
  • 8. Initializing an Array (cont'd.) • Initializer list examples int[] myScores = new int[5] {100, 76, 88, 100, 90}; int[] myScores = new int[] {100, 76, 88, 100, 90}; int[] myScores = {100, 76, 88, 100, 90}; Microsoft Visual C# 2010, Fourth Edition 8
  • 9. Accessing Array Elements • The power of arrays becomes apparent when you use subscripts – Can be variables rather than constant values • Using a loop to perform arithmetic on each element for (int sub = 0; sub < 5; ++sub) myScores[sub] += 3; Microsoft Visual C# 2010, Fourth Edition 9
  • 10. Using the Length Property • Length property – Member of the System.Array class – Automatically holds an array’s length • Examples int[] myScores = {100, 76, 88, 100, 90}; Console.WriteLine(“Array size is {0}”, myScores.Length); for (int x = 0; x < myScores.Length; ++x) Console.WriteLine(myScores[x]); Microsoft Visual C# 2010, Fourth Edition 10
  • 11. Using foreach • foreach statement – Cycles through every array element without using a subscript – Uses a temporary iteration variable • Automatically holds each array value in turn • Example double[] payRate = {6.00, 7.35, 8.12, 12.45, 22.22}; foreach(double money in payRate) Console.WriteLine(“{0}”, money.ToString(“C”)); Microsoft Visual C# 2010, Fourth Edition 11
  • 12. Using foreach (cont'd.) • Used under the following circumstances – When you want to access every array element – Since the iteration variable is read-only • You cannot assign a value to it Microsoft Visual C# 2010, Fourth Edition 12
  • 13. Using foreach with Enumerations Microsoft Visual C# 2010, Fourth Edition 13
  • 14. Searching an Array Using a Loop • Searching options – Using a for loop – Using a while loop Microsoft Visual C# 2010, Fourth Edition 14
  • 15. Using a for Loop to Search an Array • Use a for statement to loop through the array – Set a Boolean variable to true when a match is found • Solution is valid even with parallel arrays Microsoft Visual C# 2010, Fourth Edition 15
  • 16. Using a for Loop to Search an Array (cont'd.) Microsoft Visual C# 2010, Fourth Edition 16
  • 17. Using a while Loop to Search an Array • Use a while loop to search for a match Microsoft Visual C# 2010, Fourth Edition 17
  • 18. Microsoft Visual C# 2010, Fourth Edition 18
  • 19. Searching an Array for a Range Match • Range match – Determines the pair of limiting values between which a value falls Microsoft Visual C# 2010, Fourth Edition 19
  • 20. Searching an Array for a Range Match (cont'd.) Microsoft Visual C# 2010, Fourth Edition 20
  • 21. Using the BinarySearch(), Sort(),and Reverse() Methods • System.Array class contains a variety of useful, built-in methods that can search, sort, and manipulate array elements Microsoft Visual C# 2010, Fourth Edition 21
  • 22. Using the BinarySearch() Method • BinarySearch() method – Finds a requested value in a sorted array – Member of the System.Array class • Do not use BinarySearch() under these circumstances – If your array items are not arranged in ascending order – If your array holds duplicate values and you want to find all of them – If you want to find a range match rather than an exact match Microsoft Visual C# 2010, Fourth Edition 22
  • 23. Using the BinarySearch() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 23
  • 24. Using the Sort() Method • Sort() method – Arranges array items in ascending order – Use it by passing the array name to Array.Sort() Microsoft Visual C# 2010, Fourth Edition 24
  • 25. Using the Sort() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 25
  • 26. Using the Reverse() Method • Reverse() method – Reverses the order of items in an array – Element that starts in position 0 is relocated to position Length – 1 – Use it by passing the array name to the method Microsoft Visual C# 2010, Fourth Edition 26
  • 27. Using the Reverse() Method (cont'd.) Microsoft Visual C# 2010, Fourth Edition 27
  • 28. Using Multidimensional Arrays • One-dimensional or single-dimensional array – Picture as a column of values – Elements can be accessed using a single subscript • Multidimensional arrays – Require multiple subscripts to access the array elements – Two-dimensional arrays • Have two or more columns of values for each row • Also called rectangular array, matrix, or a table Microsoft Visual C# 2010, Fourth Edition 28
  • 29. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 29
  • 30. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 30
  • 31. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 31
  • 32. Using Multidimensional Arrays (cont'd.) • Three-dimensional array Microsoft Visual C# 2010, Fourth Edition 32
  • 33. Using Multidimensional Arrays (cont'd.) • Jagged array – One-dimensional array in which each element is another one-dimensional array – Each row can be a different length Microsoft Visual C# 2010, Fourth Edition 33
  • 34. Using Multidimensional Arrays (cont'd.) Microsoft Visual C# 2010, Fourth Edition 34
  • 35. Array Issues in GUI Programs • If array values change based on user input – Array must be stored outside any method that reacts to the user’s event Microsoft Visual C# 2010, Fourth Edition 35
  • 36. Array Issues in GUI Programs (cont'd.) Microsoft Visual C# 2010, Fourth Edition 36
  • 37. You Do It • Activities to explore – Creating and Using an Array – Using the Sort() and Reverse() Methods Microsoft Visual C# 2010, Fourth Edition 37
  • 38. Summary • An array is a list of data items – All of which have the same type and the same name – Items are distinguished using a subscript or index • In C#, arrays are objects of a class named System.Array • The power of arrays becomes apparent when you begin to use subscripts • Subscript you use remains in the range of 0 through length -1 Microsoft Visual C# 2010, Fourth Edition 38
  • 39. Summary (cont'd.) • foreach statement cycles through every array element without using subscripts • You can compare a variable to a list of values in an array • You can create parallel arrays to more easily perform a range match • The BinarySearch() method finds a requested value in a sorted array • The Sort() method arranges array items in ascending order Microsoft Visual C# 2010, Fourth Edition 39
  • 40. Summary (cont'd.) • The Reverse() method reverses the order of items in an array • Multidimensional arrays require multiple subscripts to access the array elements • Types of multidimensional arrays – Two-dimensional arrays (rectangular arrays) – Three-dimensional arrays – Jagged arrays Microsoft Visual C# 2010, Fourth Edition 40