-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayListInJava.java
71 lines (55 loc) · 2.84 KB
/
ArrayListInJava.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package arraylistinjava;
import java.util.ArrayList;
public class ArrayListInJava {
public static void main(String[] args) {
//declaring the arraylist
ArrayList<String> arrayList=new ArrayList<String>();
//adding value to the arraylist
arrayList.add("Shakil");
arrayList.add("Jahid");
arrayList.add("Rashed");
arrayList.add("Sagor");
arrayList.add("Faisal");
arrayList.add("koushik");
arrayList.add("Shuvo");
System.out.println("Names are : "+arrayList+"\n");
//adding an another index at 0
System.out.println("Names in arraylist are:"+arrayList);
arrayList.add(0,"Sakhawat Hossain");
System.out.println("After changing new names are : "+arrayList+"\n");
//removinf by indexing
System.out.println("Names in arraylist are:"+arrayList);
arrayList.remove(0);
System.out.println("After removing in indexing new names are : "+arrayList+"\n");
//removing by an object
arrayList.remove("Sagor");
System.out.println("Names in arraylist are:"+arrayList);
System.out.println("After removing with object new names are :"+arrayList+"\n");
//updating
arrayList.set(1,"Sakhawat Shakil");
System.out.println("Names in arraylist are:"+arrayList);
System.out.println("Printing the updated arraylist : "+arrayList+"\n");
//getting index of an object
//if the object doesnot appear in the arraylist then it will return -1
int length=arrayList.indexOf("Shakil");
System.out.println("Names in arraylist are:"+arrayList);
System.out.println("The index of desire object is :"+length+"\n");
//getting an object by the help of index
String string=arrayList.get(0);
System.out.println("Names in arraylist are :"+arrayList);
System.out.println("The desire object is :"+string+"\n");
//getting the arraylist size
int sizeOfTheArray=arrayList.size();
System.out.println("Names in arraylist are :"+arrayList);
System.out.println("The dsize of the arraylist is :"+sizeOfTheArray+"\n");
//confirming that a value is in the list or not
//and if it appears in the arraylist it will return true else false
System.out.println("Names in arraylist are:"+arrayList);
System.out.println(""+arrayList.contains("Shakil"));
System.out.println(""+arrayList.contains("Shaki"));
//deleting or clearing all the elements in the arraylist
System.out.println("Names in arraylist are:"+arrayList);
arrayList.clear();
System.out.println("After clearing :"+arrayList);
}
}