1 / 36

Arrays

Arrays. Ch 8. Array. A group of variables (elements) of the same type Reference type!. Declaring & Creating an Array. Declaring an array reference double[] hours; Instantiating array object using new hours = new double[5];. Initializing an Array.

everetta
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays Ch 8

  2. Array • A group of variables (elements) of the same type • Reference type!

  3. Declaring & Creating an Array • Declaring an array reference double[] hours; • Instantiating array object using new hours = new double[5];

  4. Initializing an Array • Declaring, instantiating, and initializing double[] hours; hours = new double[5] {10.5, 21.5, 45, 37, 40}; • Simplified syntax double[] hours = {10.5, 21.5, 45, 37, 40};

  5. Memory Allocation ?[4] 40 ?[3] 37 ?[2] 45 ?[1] 21.5 ?[0] 10.5 hours

  6. Elements of an Array • Indexed starting at 0, not 1! • The Length property tells the number of elements for(int i=0; i<hours.Length; i++) total += hours[i];

  7. foreach Loop • Loops through the elements of an array or collection • Simpler than for loop foreach(double h in hours) total += h;

  8. Array Example

  9. Result

  10. Implicitly Typed Variable • Indicated by keyword var, rather than a particular type • The compiler infers the type of the variable. • Example for (var counter=0; counter<array.Length; counter++) • The compiler infers the type of counter is int, because it’s initialized with an int value.

  11. Passing Arrays or Array Elements to Methods • Array is a reference type! • Type of array elements may be a value type or reference type • double[] hours; • GradeBook[] gradeBooks;

  12. Passing Arguments • Value type, pass-by-value • Caller's variable CANNOT be changed • Value type, pass-by-reference • Caller's variable CAN be changed • Reference type, pass-by-value • Caller's object content CAN be changed • Caller's reference CANNOT • Reference type, pass-by-reference • Both caller's object and reference CAN be changed

  13. Example: Passing Arrays or Array Elements

  14. Result

  15. Passing Arrays by Value and by Reference • Passing by value • Caller's array elements CAN be changed • Caller's array reference CANNOT • Passing by reference • Both caller's array reference and elements CAN be changed

  16. Passing Arrays by Value and by Reference

  17. Result

  18. Multidimensional Arrays • Two-dimensional • Rectangular array • Jagged array • High dimensional arrays are rare

  19. Rectangular Array • Has m rows and n columns int[,] a = new int[3, 4];

  20. Jagged Array • An array of arrays • A one-dimensional array • Each element refers to a 1D array • Rows of different lengths

  21. Jagged Array Example int[][] jagged = {new int[] {1, 2}, new int[] {3}, new int[] {4, 5, 6 } };

  22. Rectangular and Jagged Arrays

  23. Result

  24. Summary • Array • Group of elements of the same type • Passing array or array element to method • By value • By reference • Multidimensional array • Rectangular or Jagged

More Related