# 1D Array - 3
### Drawbacks of arrays
1. Size of an array is fixed.
2. Once created you can't increase or decrease the size.
3. If we want dynamic size list then we can use ArrayList.
## Array List
- **Dynamic Resizing**: ArrayList grows dynamically as we add elements to the list or shrinks as we remove elements from the list.
- **Ordered**: ArrayList preserves the order of the elements i.e. the order in which the elements were added to the list.
- **Index based**: ArrayList in Java supports indexing same as an Array.
Syntax :
```java
ArrayList<DataType> name = new ArrayList<>();
```
Explanation :
```
Here a new array list of given DataType is created.
We can create array list of long, double, float and strings as well
```
Code :
```java
ArrayList<Integer> arr = new ArrayList<Integer>();
System.out.println(arr);
```
Output :
```
[]
```
Explanation :
```
A new empty arraylist of Integers is created.
```
## Arraylist methods
1. Adding new elements to our Arraylist :
> Syntax: arraylist_name.add(element)
> Adds a specific element at the end of ArrayList.
Code :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
System.out.println(arrlist);
```
Output :
```java
[45, 5, 3]
```
Explanation :
```
Here a new array list is created and elements 45, 5, and 3 are added in order.
```
2. Finding size of arraylist :
> Syntax : arrlist.size()
> Returns the number of elements present in the ArrayList.
Code :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
System.out.println(arrlist.size());
```
Output :
```
3
```
Explanation :
```
Since we added 3 elements hence size is 3.
```
3. Accessing element at an index
> Syntax: arrlist.get(int index)
> Returns the element at the specified index in the ArrayList.
Code1 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
System.out.println(arrlist.get(1));
```
Output :
```
5
```
Explanation :
```
arrlist : [45, 5, 3] : Element at index 1 is 5.
```
Code2 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
System.out.println(arrlist.get(5));
```
Output :
```
error: Index 5 out of bounds for length 3
```
Explanation :
```
There is no such index as 4 in our arraylist hence error.
```
- Note : If Index is not present in Arraylist then it will give index out of bound error
4. Change Existing element :
> Syntax : arrlist.set(int index, new element)
> This method is used to replace the element at the specified index in the ArrayList with the specified element.
Code1 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.set(1, 19);
System.out.println(arrlist);
```
Output :
```
[45, 19, 3]
```
Explanation :
```
Here index 1 element was 5 and it is replaced by 19.
```
Code2 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.set(4, 19);
System.out.println(arrlist);
```
Output :
```
error: Index 4 out of bounds for length 3
```
Explanation :
```
There is no such index as 4 in our arraylist hence error.
```
- Note : If Index is not present in Arraylist then it will give index out of bound error
5. Remove elements :
> Syntax : arrlist.remove(int index)
> Removes the element present at specified index from the ArrayList.
Code1 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.remove(2);
System.out.println(arrlist);
```
Output :
```
[45, 5]
```
Explanation :
```
3 was present at index 2 hence it is removed.
```
Code2 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.remove(4);
System.out.println(arrlist);
```
Output :
```
error: Index 4 out of bounds for length 3
```
Explanation :
```
There is no such index as 4 in our arraylist hence error.
```
- Note : If Index is not present in Arraylist then it will give index out of bound error
6. Membership check
> Syntax : array_name.contains(Object element)
> Returns true if the ArrayList contains the specified element.
Code1 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.contains(4);
System.out.println(arrlist);
```
Output :
```
false
[45, 5, 3]
```
Explanation :
```
Our arraylist does not have element 4 hence output is false
```
Code2 :
```java
ArrayList<Integer> arrlist = new ArrayList<>();
arrlist.add(45);
arrlist.add(5);
arrlist.add(3);
arrlist.contains(3);
System.out.println(arrlist);
```
Output :
```
true
[45, 5, 3]
```
Explanation :
```
Our arraylist does not have element 3 hence output is true
```
- If you want to get more information about arraylist methods use this link: https://www.scaler.com/topics/java/arraylist-in-java/