# Arrays vs ArrayLists

Both Arrays and ArrayLists are incredibly useful to programmers, but how do we know when to use them? While both Arrays and ArrayLists perform similar operations, they are used differently.

## Differences Between Arrays and ArrayLists

One the biggest differences between an Array and ArrayList is expandability. While the size of an ArrayList can change, an Array is a set size. Other differences include handling types and getting the size/length.

### Getting the Size or Length:

How we retrieve the size or length of an Array or ArrayList varies between the two.

When dealing with an ***Array*** we use `arr.length` to access its length.

With ***ArrayLists*** we would use `list.size()`.

### Setting Values at an Index:

Settings values at a given index varies as well.

To set the value of a given index with an ***Array*** we use `arr[i] = x;`.

To set the value of a given index with an ***ArrayList*** we use `list.set(i, x);`.

### Getting Values at an Index:

To retrieve values at a given index is as follows:

To get a value at a given index with an ***Array*** we use `int x = arr[i];`.

To get a value at a given index with an ***ArrayList*** we use `int x = list.get(i);`.

### Creating New Instances:

To create new instances of an Array or ArrayList you use:

To create a new instance of an ***Array*** we use `int[] arr = new int[5];`.

To create a new instance of an ***ArrayList*** we use

```

ArrayList<Integer> list = new ArrayList<Integer>();
```

### Extra Helper Methods:

Only ArrayLists have extra helper methods. These helper methods include: remove, add at index, clear, and isEmpty.

### Types:

***Arrays*** can hold Primitives or Objects.

***ArrayLists*** can only hold Objects, and handles autoboxing and unboxing.


---

# Agent Instructions: 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:

```
GET https://codehs.gitbook.io/apjava/data-structures/arrays-vs-arraylists.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
