-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arraylist.java
59 lines (45 loc) · 1.13 KB
/
Arraylist.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
package core;
import java.util.ArrayList;
import java.util.Collections;
public class Arraylist {
public static void main(String[] args) {
ArrayList<String> phones = new ArrayList<String>();
phones.add("Apple");
phones.add("Samsung");
phones.add("Moto");
phones.add("Spark");
phones.add("Oppo");
System.out.println(phones);
//Check If an Item Exists
System.out.println(phones.contains("Moto"));
//Access an item
System.out.println(phones.get(1));
//Change an item
phones.set(3, "Vivo");
System.out.println(phones);
//Remove an item
phones.remove(2);
System.out.println(phones);
//get Size of array
System.out.println(phones.size());
//Loop through an ArrayList
for(int i=0;i<phones.size();i++)
{
System.out.println(phones.get(i));
}
//foe-each loop
for(String i:phones)
{
System.out.println(i);
}
//Sort an ArrayList (import java.util.Collections;)
Collections.sort(phones);
for(String i:phones)
{
System.out.println(i);
}
//clear ArrayList
phones.clear();
System.out.println(phones);
}
}