-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathArrayListExec.java
More file actions
65 lines (45 loc) · 1.61 KB
/
ArrayListExec.java
File metadata and controls
65 lines (45 loc) · 1.61 KB
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
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExec {
public static void main(String[] args) {
ArrayList<Car> carsList = new ArrayList<Car>();
Car c = new Car("H234");
carsList.add(c);
System.out.println(carsList.size());
//add(int index, E e)
for(int i=0; i<5; i++){
carsList.add(new Car("H"+i));
}
carsList.add(4, new Car("H345"));
//Iterator
Iterator<Car> carIterator = carsList.iterator();
while(carIterator.hasNext()){
System.out.println(carIterator.next());
}
//Clone method
ArrayList<Car> carsList2 = (ArrayList<Car>)carsList.clone();
Iterator<Car> carsIterator2 = carsList2.iterator();
while (carsIterator2.hasNext()) {
System.out.println("----"+carsIterator2.next());
}
//Contains(Object o)
System.out.println(carsList.contains(new Car("H234")));
//get(int index)
System.out.println(carsList.get(4));
// if(carsList.size() == 0) // Bad practice
//use
//if(carsList.isEmpty()){}
//Loop and size
//for(int i =0; i<carsList.size(); i++){} //Bad Practice
//for(int i =0 , n = carsList.size(); i<n ; i++){}//Good to get the size and have in a local variable
//indexOf(Object o)
System.out.println(carsList.indexOf(new Car("H4")));
ArrayList<Car> carsList3 = new ArrayList<Car>();
carsList3.ensureCapacity(300);
long startTime = System.nanoTime();
for(int i =0; i<100; i++){
carsList3.add(new Car("H"+(i+10)));
}
System.out.println("Elapsed Time - no ENSURE CAPACITY- "+( System.nanoTime()-startTime));
}
}