Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift 2.0 Collections and Control Flow Introduction to Collections Working with Arrays

Kevin Coleman
Kevin Coleman
1,123 Points

How is 6 not the 5th value? If arrays are numbered from 0, shouldnt 6 be the 5th value?

Not sure if im looking at it the wrong way. Objective is asking for the 5th value in the Array.

arrays.swift
// Enter your code below
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts += [8]
let value = arrayOfInts[6]

2 Answers

Steven Deutsch
Steven Deutsch
21,046 Points

Hey Kevin Coleman,

You are correct about how indexes are numbered, however, you are passing in the wrong index. Indexes are numbered starting from 0, so to access the 5th item in the array, you need to pass in an index of 4.

// Enter your code below
var arrayOfInts = [1, 2, 3, 4, 5, 6]
arrayOfInts.append(7)
arrayOfInts += [8]
/* You specify the index for what item you want to access
inside of the brackets. */
let value = arrayOfInts[4]

Good Luck!

Kevin Coleman
Kevin Coleman
1,123 Points

Ahhh i see. So im counting from 0 but starting at "1" being 0, when i should be counting from 0 with "1" being 2. Right? Steven Deutsch

Steven Deutsch
Steven Deutsch
21,046 Points

Take a look at the following code and see if this helps clear things up.

var arrayOfInts = [1, 2, 3, 4, 5, 6]
/* 
 arrayOfInts[0] is the first item, "1"
 arrayOfInts[1] is the second item, "2"
 arrayOfInts[2] is the third item, "3"
 arrayOfInts[3] is the fourth item, "4"
 arrayOfInts[4] is the fifth item, "5"
 arrayOfInts[5] is the sixth item, "6"
*/

The first item in the array will always have an index of 0. The last item in the array will always have an index that is one less than the total number of items in the array.

Kevin Coleman
Kevin Coleman
1,123 Points

That actually helps a lot! Makes perfect sense now. Thanks! Steven Deutsch