← Back to Swift Course | Chapter 6: Collections | Lesson 1 of 7

Arrays

An array is an ordered list of things, like a row of lockers each holding one item, numbered starting from zero.
Syntax
swift
var name = [value1, value2, value3]
name[0]
name.append(value)
name.remove(at: index)
name.count
name.isEmpty
name.contains(value)

Creating and Accessing Arrays

An array is created with square brackets containing comma-separated values, and its elements are accessed by zero-based integer index.

Example: Creating and Accessing Arrays

markup
// Declare a mutable variable `fruits`, initialized to `["apple", "banana", "cherry"]`
var fruits = ["apple", "banana", "cherry"]
// Print `fruits[0]` to the console
print(fruits[0])
// Print `fruits[2]` to the console
print(fruits[2])
// Print "Total fruits: \(fruits.count)" to the console
print("Total fruits: \(fruits.count)")

Modifying an Array

Elements can be added with .append(), removed with .remove(at:), and updated by assigning directly to an index.

Example: Modifying an Array

markup
// Declare a mutable variable `numbers`, initialized to `[1, 2, 3]`
var numbers = [1, 2, 3]
// Append `4` to `numbers`
numbers.append(4)
numbers.remove(at: 0)
numbers[0] = 99
// Print `numbers` to the console
print(numbers)

Checking Array Contents

Properties like .isEmpty and .count, along with methods like .contains(), help inspect an array without manual loops.

Example: Checking Array Contents

markup
// Declare a constant `scores`, set to `[80, 95, 70]`
let scores = [80, 95, 70]
// Print "Is empty: \(scores.isEmpty)" to the console
print("Is empty: \(scores.isEmpty)")
// Print "Count: \(scores.count)" to the console
print("Count: \(scores.count)")
// Print "Contains 95: \(scores.contains(95))" to the console
print("Contains 95: \(scores.contains(95))")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to access an index that's out of bounds, e.g. array[10] on a 3-element array, which crashes at runtime instead of returning nil.
  2. Forgetting that arrays declared with let are immutable -- you cannot append or remove elements from a constant array.
  3. Confusing .count (number of elements) with the last valid index, which is .count - 1.
Chapter Summary
  • An array is an ordered, indexable collection of values of the same type, written as [Type].
  • Arrays declared with var can be modified; those declared with let cannot.
  • Common operations include .append(), .remove(at:), .count, and indexing with [].
  • Accessing an out-of-bounds index crashes the program at runtime.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.