Arrays

7. Arrays#

A week of sales figures is easier to process as one indexed group than as seven unrelated variables. An array is a collection type with a fixed length. This chapter connects element access and loop traversal to aggregation, shared references, method calls, searching, sorting, and rectangular data; Chapter 8 broadens the choice of collection families.

  • Array: An array stores a fixed number of same-typed values under one name.

  • Index: An index selects a specific element by position.

  • Traversal: Loops let a program visit each array element systematically.

  • Searching and sorting: A linear search scans positions in order for a match; a sort puts elements in order so that binary search can find a value by halving the candidates.

  • Two-dimensional array: A 2D array models grid-like data such as tables, boards, and matrices.

Chapter Video

C# arrays (5:57) by Bro Code.

Hosted on YouTube; property of its original creator, embedded here for educational reference only.

Learning Objectives

By the end of this chapter, you will be able to:

  1. Explain why an array is a collection, then declare, initialize, and access one-dimensional arrays within their valid index ranges.

  2. Traverse arrays with for and foreach to calculate results and update elements where appropriate.

  3. Trace array reference assignment, and distinguish sharing a reference from making a separate copy with Clone, Array.Copy, or a new array.

  4. Pass arrays to methods and return array results while explaining which operations can change the caller’s elements, and why reassigning a parameter cannot.

  5. Implement a first-match linear search that returns an index or the sentinel -1, and adapt the scan to a different matching rule, a count of matches, or the largest element.

  6. Sort a clone with Array.Sort using a stated comparer and search it with Array.BinarySearch, explaining the sorted precondition and the sign test on the result.

  7. Trace and implement binary search on a half-open interval and insertion sort with a saved item, and test both at empty, single-element, first, last, and missing cases.

  8. Declare and process rectangular and jagged arrays using the correct bounds for each dimension, including row totals, column totals, and passing a table to a method.

Chapter Flow

Suggested three-meeting pace (110 minutes): read the relevant explanations and predict the selected example before class.

Meeting

Minutes

Guided focus

1

35

creation, indexing, default values and bounds errors, accumulating over an array, aliases, and copies.

2

40

search intervals, insertion-sort trace, and defect repair.

3

35

rectangular tables, row and column totals, passing tables to methods, and jagged arrays.

Class uses selected traces, runs, and checks. Use the remaining worked examples and detailed API tables for practice and reference. Supporting readings are selected separately; lab and homework follow the core lessons.

Glossary

#

Term

Explanation

1

Array

A fixed-size collection of values stored in indexed order.

2

Element

One value stored inside an array.

3

Index

A zero-based position used to access an array element.

4

Length

The number of elements an array can hold.

5

Initializer

Syntax that creates an array with starting values.

6

Traversal

Visiting each element in a collection.

7

For loop traversal

Array processing that uses an index variable.

8

Foreach traversal

Array processing that visits each element directly.

9

Bounds

The valid index range from 0 through Length - 1.

10

Out-of-range error

An error caused by using an invalid index.

11

Two-dimensional array

A rectangular array addressed by row and column indexes.

12

Row

A horizontal group of values in a two-dimensional array.

13

Column

A vertical group of values in a two-dimensional array.

14

Array parameter

A method parameter whose value is an array reference.

15

Alias

Another reference to the same array object.

16

Shallow copy

A separate array containing copied element values or references, such as the result of Clone or Array.Copy, without cloning the objects those references reach.

17

Rectangular array

An array with a fixed length for each dimension and the same row width throughout.

18

Linear search

Checking elements in index order and stopping at the first match.

19

Sentinel

A return value such as -1 that cannot be a real index and signals a missing result.

20

Search contract

The promised result of a search: present or not, first index, or a count of matches.

21

In-place sort

A sort such as Array.Sort that rearranges the array it is given, visible through every alias.

22

Sorted precondition

The requirement that Array.BinarySearch receive an array already sorted with the same comparer.

23

Binary search

Finding a value in a sorted array by repeatedly discarding the half that cannot contain it.

24

Ordinal comparison

Ordering or matching strings by character codes, independent of culture settings.

25

Insertion sort

A sort that keeps the front of the array ordered and inserts each next element into its place.

26

Half-open interval

A candidate region [low, high) that includes low and excludes high; empty when low equals high.

27

Saved item

The element copied aside before shifting so that the shifts cannot overwrite it.

28

Precondition

A requirement that must hold before an operation is used, such as sorted input for binary search.

Lecture Slides

Chapter 7 lecture slides follow the current reading order. Supporting parts remain optional; instructor notes and editable decks are distributed separately.