> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/useful-java-knowledge/arraylist-vs-array.md).

# ArrayList vs Array

### Initialization

{% tabs %}
{% tab title="ArrayList" %}

```java
ArrayList<Integer> a = new ArrayList<>();
ArrayList<Integer> a = new ArrayList<>(5);
```

{% endtab %}

{% tab title="Array" %}

```java
int[] a = new int[5];
char[][] c = new char[3][];
c[0] = new char[5];
c[2] = new char[9];

String[] arr = {"a", "b", "c"};
```

{% endtab %}
{% endtabs %}

### Useful methods

{% tabs %}
{% tab title="ArrayList" %}

```java
l = a.size();
temp = a.get(index); // O(1)
temp = a.set(index, val); // O(1)
a.get(index); // O(1)
a.add(index, value); // O(n)
a.add(value); // O(1), add to end
a.set(index, value); // O(1)
a.remove(index); // O(n)
a.remove(val); // O(n)
a.find(val);   // O(n)
a.contains(num); // O(n)
```

{% endtab %}

{% tab title="Array" %}

```java
a.length;
c.length;
char temp = c[3][4];

```

{% endtab %}
{% endtabs %}
