Skip to content

Markdown file added #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 180 additions & 0 deletions lessons/arraylist.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,183 @@ order: "4B"
section: "Arrays"
description: "learn arrays"
---

The ArrayList class is in implementation of the list interface that allow us to create resizable array. In simple term we can say that ArrayList in java is dynamic array for storing the elements. It is just like the vector in c++.


## Creating An ArrayList

```java
ArrayList<type>arraylist = new ArrayList<> ;
//here type define any type of ArrayList.
Example:
ArrayList<Integer> arrlist = new ArrayList<Integer>(n);

```

## Code Snippet to demonstrate the working of ArrayList in Java
```java
import java.io.*;
import java.util.*;

class ArrayListExample {
public static void main(String[] args)
{
//create array
ArrayList<Integer> arr = new ArrayList<Integer>(n);

arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
System.out.println("List: " + arr );
}
}

```

## Output
```java
List : [1, 2, 3, 4]
```

## Methods OF ArrayList :
Some Commonly used Methods

Method | Detail
------------- | -------------
size() | give the size of the ArrayList
sort() | to sort the elements in the list
isEmpty() | Checks if the arraylist is empty.
indexOf() | to find index of any element.



## Basic Operation in Java ArrayList :
The ArrayList class have some methods to perform different operations on arraylists. Let's see some of most used methods.

- Add elements
- Access elements
- Change elements
- Remove elements

### Add Elements:
- add() : This is the method to insert any elements at the end of the list.
```java
import java.util.ArrayList;

class Main {
public static void main(String[] args){

ArrayList<String> playerName = new ArrayList<>();

// add() method
playerName.add("Virat");
playerName.add("Dhoni");
playerName.add("Sachin");
//print
System.out.println("ArrayList: " + playerName);
}
}
```
### Output:

```
ArrayList : [Virat,Dhoni,Sachin]
```

- get() : This method is used to acess the elements from the list.

```java
import java.util.ArrayList;

class Main {
public static void main(String[] args){

ArrayList<String> playerName = new ArrayList<>();


playerName.add("Virat");
playerName.add("Dhoni");
playerName.add("Sachin");
System.out.println("ArrayList: " + playerName);

// get() method
String name1 = playerName.get(1);
System.out.println(name1);
}
}

```
### Output:
```
ArrayList : [Virat,Dhoni,Sachin];
Dhoni
```
- set(): This method is used to set any elements at any position.

```java
import java.util.ArrayList;

class Main {
public static void main(String[] args) {
ArrayList<String> playerName = new ArrayList<>();

// add() method without the index parameter
playerName.add("Virat");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

playerName is not defined??

playerName.add("Dhoni");
playerName.add("Sachin");
System.out.println("ArrayList: " + playerName);


// change the element of the array list
playerName.set(2, "Rohit");
System.out.println("New ArrayList: " + playerName);
}
}
```
### Output:
```
ArrayList : [Virat,Dhoni,Sachin]
New ArrayList : [Virat,Dhoni,Rohit]

```
- remove() : To remove the elements from the list

```java
import java.util.ArrayList;

class Main {
public static void main(String[] args) {
ArrayList<String> playerName = new ArrayList<>();


// add() method without the index parameter
playerName.add("Virat");
playerName.add("Dhoni");
playerName.add("Sachin");
System.out.println("ArrayList: " + playerName);

// remove element
String name1 = playerName.remove(2);
//print list after removal
System.out.println("New ArrayList: " + playerName);
//removed elements
System.out.println("Removed: " + name1);
}
}
```
### Output:
```
ArrayList : [Virat,Dhoni,Sachin]
New ArrayList : [Virat,Dhoni]
Removed: Sachin
```
## Some remembering points about ArrayList

- It comes under java.util package.
- This class maintains insertion order.
- It allows random access.
- ArrayList is not Synchronized
- To convert ArrayList into an array we use toArray() method.
- To converst array into an arraylist we use asList() method.