> For the complete documentation index, see [llms.txt](https://ios-course.cornellappdev.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ios-course.cornellappdev.com/resources/swift-foundations/arrays/basic-array-operations.md).

# Basic Array Operations

#### Count

The length of your array is accessible through the count field. To access it, just do `myArray.count`.

#### Get

Getting a single element is standard–it's your array followed by square brackets that contain the index of the element you want. Of course, make sure it is within the bounds of the array or else you will have a runtime error. Lists are 0-indexed meaning that the bounds for any list is 0 to one less than the length of the list.

```swift
let myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]

myList[0]
"a": String

myList[myList.count - 1]
"c": String

myList[-1]
// Fatal error: Index out of range
myList[myList.count]
// Fatal error: Index out of range
```

If you want to get a section of the array, or an `ArraySlice`, you need to use ranges. See the ranges page for their construction.

{% embed url="<https://app.gitbook.com/o/-LDzut-Pw60HMkLc_mHP/s/-Lwk7443W4ukbAF9S07e/c/io62dSCeIcKzFkdzUsyz/swift-guide-under-construction/ranges>" %}
Ranges
{% endembed %}

This range must correspond to indices that will be used in the array slice.

The section you specify will be used to create a new `ArraySlice`, which contains the information with limited functionality of arrays. To get this functionality back use the the `Array(_: Sequence)` initializer to convert back to an array.

```swift
let myList = ["a", "b", "c", "d", "e"]
// myList: [String] = ["a", "b", "c", "d", "e"]

let mySlice = myList[3..<myList.count]
// mySlice: ArraySlice<String> represents ["d", "e"]

let myNewList = Array(mySlice)
```

There are two more ways to get elements from an array, which are more advanced because they require an understanding of anonymous functions and optionals.

#### Put

We use subscript assignments to assign elements to pre-existing locations.

```swift
var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList[0] = "A"
// myList: [String] = ["A", "b", "c"]
```

To add a new element at the end of a list, or to add a new element when order does not matter, use the `append(_: element)` method.

```swift
var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.append("A")
// myList: [String] = ["a", "b", "c", "A"]
```

To add an element at a specific location, we use the `insert(_: element, at: Int)` method. This method will shift every element after this location up by one, and then insert the element at the location:

```swift
var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.insert("A", at: 1)
// myList: [String] = ["a", "A", "b", "c"]
```

#### Removal

To remove an element at a specific location use `remove(at: Int)`.

```swift
var myList = ["a", "b", "c"]
// myList: [String] = ["a", "b", "c"]
myList.remove(at: 1)
// myList: [String] = ["a", "c"]
```

#### Addition

What do you do if you have two lists that you want to concatenate? One technique would be sequentially appending them–iterate through one list and sequentially add the elements of the list to the other list. Another, more efficient and clean way is just to use the `+` operator.

```swift
let lst1 = ["a", "b", "c"]
// lst1: [String] = ["a", "b", "c"]
let lst2 = ["d", "e", "f"]
// lst2: [String] = ["d", "e", "f"]
let lst3 = lst1 + lst2
// lst3: [String] = ["a", "b", "c", "d", "e", "f"]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ios-course.cornellappdev.com/resources/swift-foundations/arrays/basic-array-operations.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
