["Hip", "hip", "array!"] We have learned about how to store groups of data into arrays in Swift. 🙌
Here are some of the things that we covered:
- An array is a list of ordered items.var evenNumbers = [2, 4, 6, 8, 10]
- The first index in an array is 0.
.countproperty that returns the size of an array.
- Some of the methods that come with arrays:
.append(): adding an item to the end
.insert(): adding an item to an index
.remove(): removing an item from an index
inloop can be used to iterate through an array.
Download the Arrays: Cheat Sheet for referencing the content covered in this lesson.
Optional: Write a Swift program to find the sum of even numbers and the product of odd numbers in an array.
For example, suppose we have an array that is
[2, 4, 3, 6, 1, 9], then the program should output:
Sum of even numbers is 12 Product of odd numbers is 27