forked from Anuj-3107/HACKTOBER2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraylist.java
More file actions
34 lines (28 loc) · 915 Bytes
/
Copy pathArraylist.java
File metadata and controls
34 lines (28 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
class Test{
public static void main(String[] args) {
ArrayList<String> name=new ArrayList<>();
name.add("Sahil");
name.add("Sunil");
name.add("Potdukhe");
System.out.println("Name is:"+name);
name.add(1,"Hero");
System.out.println("Name is:"+name);
name.remove(2);
System.out.println("Name is:"+name);
name.set(2,"Indian");
System.out.println("Name is:"+name);
System.out.println(name.get(1));
//print all the elements
for(String str: name){
System.out.println(str);
}
//Convert arraylist to array
String[] arr=new String[name.size()];
name.toArray(arr);
System.out.print("Array: ");
for(String item: arr){
System.out.print(item+" ");
}
}
}