Skip to content

Commit 9dcea22

Browse files
committed
md file added
1 parent 0a9be2f commit 9dcea22

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

lessons/arraylist.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,184 @@ order: "4B"
55
section: "Arrays"
66
description: "learn arrays"
77
---
8+
## ArrayList in java
9+
<hr>
10+
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.
11+
It is just like the vector in c++.
12+
13+
14+
## Creating An ArrayList
15+
16+
```java
17+
ArrayList<type>arraylist = new ArrayList<> ;
18+
//here type define any type of ArrayList.
19+
Example:
20+
ArrayList<Integer> arrlist = new ArrayList<Integer>(n);
21+
22+
```
23+
24+
## Code Snippet to demonstrate the working of ArrayList in Java
25+
```java
26+
import java.io.*;
27+
import java.util.*;
28+
29+
class ArrayListExample {
30+
public static void main(String[] args)
31+
{
32+
//create array
33+
ArrayList<Integer> arr = new ArrayList<Integer>(n);
34+
35+
arr.add(1);
36+
arr.add(2);
37+
arr.add(3);
38+
arr.add(4);
39+
System.out.println("List: " + arr );
40+
}
41+
}
42+
43+
```
44+
45+
## Output
46+
```java
47+
List : [1, 2, 3, 4]
48+
```
49+
50+
## Methods OF ArrayList :
51+
Some Commonly used Methods
52+
53+
Method | Detail
54+
------------- | -------------
55+
size() | give the size of the ArrayList
56+
sort() | to sort the elements in the list
57+
isEmpty() | Checks if the arraylist is empty.
58+
indexOf() | to find index of any element.
59+
60+
61+
62+
## Basic Operation in Java ArrayList :
63+
The ArrayList class have some methods to perform different operations on arraylists. Let's see some of most used methods.
64+
65+
- Add elements
66+
- Access elements
67+
- Change elements
68+
- Remove elements
69+
70+
## Add Elements :
71+
- add() : This is the method to insert any elements at the end of the list.
72+
```java
73+
import java.util.ArrayList;
74+
75+
class Main {
76+
public static void main(String[] args){
77+
78+
ArrayList<String> playerName = new ArrayList<>();
79+
80+
// add() method
81+
playerName.add("Virat");
82+
playerName.add("Dhoni");
83+
playerName.add("Sachin");
84+
//print
85+
System.out.println("ArrayList: " + playerName);
86+
}
87+
}
88+
```
89+
## Output
90+
```
91+
ArrayList : [Virat,Dhoni,Sachin]
92+
```
93+
94+
- get() : This method is used to acess the elements from the list.
95+
96+
```java
97+
import java.util.ArrayList;
98+
99+
class Main {
100+
public static void main(String[] args){
101+
102+
ArrayList<String> playerName = new ArrayList<>();
103+
104+
105+
playerName.add("Virat");
106+
playerName.add("Dhoni");
107+
playerName.add("Sachin");
108+
System.out.println("ArrayList: " + playerName);
109+
110+
// get() method
111+
String name1 = playerName.get(1);
112+
System.out.println(name1);
113+
}
114+
}
115+
116+
```
117+
## Output
118+
```
119+
ArrayList : [Virat,Dhoni,Sachin];
120+
Dhoni
121+
```
122+
- set(): This method is used to set any elements at any position.
123+
124+
```java
125+
import java.util.ArrayList;
126+
127+
class Main {
128+
public static void main(String[] args) {
129+
ArrayList<String> languages = new ArrayList<>();
130+
131+
// add() method without the index parameter
132+
playerName.add("Virat");
133+
playerName.add("Dhoni");
134+
playerName.add("Sachin");
135+
System.out.println("ArrayList: " + playerName);
136+
137+
138+
// change the element of the array list
139+
languages.set(2, "Rohit");
140+
System.out.println("New ArrayList: " + playerName);
141+
}
142+
}
143+
```
144+
## Output
145+
```
146+
ArrayList : [Virat,Dhoni,Sachin]
147+
New ArrayList : [Virat,Dhoni,Rohit]
148+
149+
```
150+
- remove() : To remove the elements from the list
151+
152+
```java
153+
import java.util.ArrayList;
154+
155+
class Main {
156+
public static void main(String[] args) {
157+
ArrayList<String> animals = new ArrayList<>();
158+
159+
160+
// add() method without the index parameter
161+
playerName.add("Virat");
162+
playerName.add("Dhoni");
163+
playerName.add("Sachin");
164+
System.out.println("ArrayList: " + playerName);
165+
166+
// remove element
167+
String name1 = playerName.remove(2);
168+
//print list after removal
169+
System.out.println("New ArrayList: " + playerName);
170+
//removed elements
171+
System.out.println("Removed: " + name1);
172+
}
173+
}
174+
```
175+
## Output
176+
```
177+
ArrayList : [Virat,Dhoni,Sachin]
178+
New ArrayList : [Virat,Dhoni]
179+
Removed: Sachin
180+
```
181+
## Some remembering points about ArrayList
182+
183+
- It comes under java.util package.
184+
- This class maintains insertion order.
185+
- It allows random access.
186+
- ArrayList is not Synchronized
187+
- To convert ArrayList into an array we use toArray() method.
188+
- To converst array into an arraylist we use asList() method.

0 commit comments

Comments
 (0)