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