# Arrays

{% hint style="info" %}
This page assumes prior knowledge of Python lists from CS61A or equivalent.
{% endhint %}

Arrays are a very popular data structure that stores an indexed list of data. <br>

![An artistic interpretation of a new int\[5\] {6, 1, 2, 3, 99};](/files/-M7-qWTNSpUm1ErSXBM7)

## Properties

* **Fixed length:** after instantiation, the length of an array cannot be changed.
* Every value in array is the **same type** and holds the **same amount of bits** in memory.
* **Zero-indexed.** That means `arr[0]` returns the first value, and `arr[arr.length]` is out of bounds.
* **No methods.** Helper methods from other libraries (like `System.arraycopy`) need to be used to manipulate arrays.
* **Retrieval is independent of size** and takes constant time regardless of how big arrays are.

## Using Arrays in Java

**Instantiation:**

* `int[] a = {1, 2, 3, 4, 5};` assigns values.
* `int b = new int[3];` creates array of provided length populated with default values.

**Copying**

* Simply assigning `int[] c = b` will copy the **pointer** to array b! Not the values! See [Java Objects](/oop/objects.md) for a discussion on why this is significant.
* Use `System.arraycopy(source, start, target, startTarget, amountToCopy)` to **shallow copy** the values (or pointers) in the array. That is, if an array is holding **reference types,** only the pointers will be copied and not the actual values of the reference objects being held.
* `System.arraycopy(b, 0, x, 3, 2)` is equivalent to `x[3:5] = b[0:2]` in Python.

**Multidimensional Arrays**&#x20;

* `int[][] 2d = new int[4][4];`or `int[][] 2d = new int[][] {{1}, {2, 3}, {4, 5, 6}};`will create **arrays inside of an array.** This is useful for storing matrices, coordinate maps, or any other multidimensional data!

**Generic Arrays**

* Arrays of generic objects are NOT allowed! Use ArrayLists instead.
* Or, this workaround can be used:`Type[] items = (Type[]) new Object[length]`

## Array Lists

Java has another built-in type that uses an array under the hood, which is the `ArrayList`. Here's how ArrayLists are different from normal arrays:

* ArrayLists can resize arbitrarily. (They use something similar to the array case study in the [Amortization](/asymptotics/amortization.md#what-if-we-doubled-the-size-instead-of-adding-one) page.
* ArrayLists use [Generic Types](/oop/generics.md) and therefore do not support primitive types like `int`.
* ArrayLists have all behaviors expected from the [Collections](/abstract-data-types/collections.md) interface.


---

# 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://cs61b.bencuan.me/abstract-data-types/collections/arrays.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.
