top of page

Array - Mommy, I Created a Shopping List


Array - A list of items with same data type, like a numbered of a grocery list.

Items in the array are stored in an index.

What is mutable?

What is immutable?

==

var nationalParks: [String] = ["Acadia", "Zio", "Grand Canyon"]

Mutable Array - You can change it, adding items, remove items, etc.

Immutable Array - Cannot be changed - huh?

When should you use mutable vs immutable?

Literal - a value exactly what you see, now a variable or constant.

==

let nationalParks: [String] = ["Acadia", "Zion", "Grand Canyon"]

print("The first part I went to was \(nationalParks[0])")

print("The second part I went to was \(nationalParks[1])")

print("Your app will crash when I access \(nationalParks[3])")

== Functions ==

nationalParks.append("Badilands")

removeLast()

remove

removeFirst, etc.

== Scoot Over Buddy ==

Let's insert between Grand Canyon and Badilands

nationalParks.insert("Golden Gate Park", at: 3)

===

== Let's Shopping ==

var fruits = ["banana", "bluberry", "mango"]

var liquids = ["orange juice", "apple juice", "milk"]

print(fruits)

print(liquids)

var smoothie = fruits + liquids

print(smoothie)

smoothie += ["whipped cream"]

print(smoothie)

=== for in ==

For in is a great way to index the array!

==========

Let's make Swift calculate a square of any number using an array.

==========

Homework:

- Create a shopping list with 5 items using an array (your choice)

- Print the full array

- Replace position 2 and 4 with milk and eggs, respectively

- Print the full array

- Delete milk

- Print the full array

- Delete the entire array

- Print the full array

- If the array is empty, print your shopping list is empty. If the array is not empty, print the array using for-in loop.

- Remember to test your if statement.

===


Featured Posts
Recent Posts
Archive
Search By Tags
No tags yet.
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
bottom of page