ProgramGuru

Arrays: keep an ordered list

Arrays: keep an ordered list

A class list has an order and may contain repeated values. A Swift Array stores elements of one type in order and supports indexed access. Its first valid index is zero; the final valid index is one less than count for a non-empty array.

What you’ll learn

  • Access and update an ordered collection within its bounds.
  • Explain the independent logical behaviour of an array copy.

Arrays: keep an ordered list in the example

  1. Two original elements

    A class list has an order and may contain repeated values.

    Two original elements
  2. Append to one value

    An array declared with var can append, replace or remove elements.

    Append to one value
  3. Independent logical copies

    The updated list contains three scores and its first element remains 70.

    Independent logical copies
The updated list contains three scores and its first element remains 70. Original still contains two elements. Swift can share storage internally until mutation, but that optimization does not change the observable value behaviour.

How arrays: keep an ordered list works

An array declared with var can append, replace or remove elements. Indexing outside its valid bounds traps; it does not return nil. The first property instead returns an optional because the array may be empty. Arrays have value semantics, so assigning an array and changing one copy preserves the other’s logical value.

Work through arrays: keep an ordered list

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

var scores = [70, 80]
var original = scores
scores.append(90)
print(scores.count)
print(scores[0])
print(original.count)

Expected output

3
70
2

Follow the execution

The updated list contains three scores and its first element remains 70. Original still contains two elements. Swift can share storage internally until mutation, but that optimization does not change the observable value behaviour.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Arrays: keep an ordered list

Your task: What is a safe way to request the first score when a list might be empty?

Swift · complete program

var scores = [70, 80]
var original = scores
scores.append(90)
print(scores.count)
print(scores[0])
print(original.count)

Worked answer

Use scores.first and handle its optional result. Direct scores[0] access requires evidence that the array is not empty.

Protect collection bounds

Insertion near the front may require moving elements. Choose a collection for its semantics and expected operations, not only because array syntax is familiar. Removing an element can also change later index positions.

Check your decision

What is a safe way to request the first score when a list might be empty?
Choose one answer. You can try again.

Answer and explanation

Use scores.first and handle its optional result.

Use scores.first and handle its optional result. Direct scores[0] access requires evidence that the array is not empty.

Protect collection bounds

Use scores.first and handle its optional result. Direct scores[0] access requires evidence that the array is not empty. Insertion near the front may require moving elements.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Dictionaries: look up a value by key →