-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDemo2.java
More file actions
90 lines (86 loc) · 2.2 KB
/
Copy pathDemo2.java
File metadata and controls
90 lines (86 loc) · 2.2 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.ArrayList;
import java.util.Collections;
/*
* AutoBoxing Feature Comes From Java 1.5
* And this is used to convert Primitive Type into Wrapper
* Type and vice-versa.
*/
import java.util.Iterator;
import java.util.ListIterator;
public class Demo2 {
static void evenOdd(int x){
if(x%2==0){
System.out.println("Even No "+x);
}
else{
System.out.println("Odd No "+x);
}
}
public static void main(String[] args) {
// 1.5 Styling
// Generics
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(100);// AutoBoxing
//list.add(new Integer(100)); // Insert at End
list.add(200);
//list.add(new Integer(200));
//list.add("amit");
//list.add(new Double(90.290));
list.add(300);
list.add(0,900); // Insert at specified index
System.out.println(list);
list.remove(1);
System.out.println(list);
// UnBoxing - Object (Wrapper) to Primitive
// Boxing - Primitive to Object(Wrapper)
list.add(list.get(1) + list.get(2)); // AutoBoxing
/*Integer firstNo = (Integer)list.get(1);
Integer secondNo = (Integer)list.get(4);
list.add(new Integer(firstNo.intValue() + secondNo.intValue()));*/
System.out.println("Now List is "+list);
// Search Operation
int index = -1;
index = list.indexOf(200);
if(index>=0){
//if(list.contains(2100)){
System.out.println("Found...");
list.set(index, 10000); // Update
System.out.println("NOw After Updation List is "+list);
//list.remove(index);
}
else
{
System.out.println("Not Found...");
}
Collections.sort(list);
System.out.println("After Sort "+list);
// Traverse
// 1st Way
for(int i = 0 ; i<list.size(); i++){
System.out.println(list.get(i));
}
// 2nd Way
Iterator<Integer> it = list.iterator();
while(it.hasNext()){
int currentElement = it.next();
//it.remove();
// it.next() give current element and then
// move to the next element
System.out.println(currentElement);
}
//3rd Way
ListIterator<Integer> lt = list.listIterator();
while(lt.hasNext()){
System.out.println(lt.next());
}
while(lt.hasPrevious()){
System.out.println(lt.previous());
}
//4th Way
for(int d : list){
System.out.println(d);
}
// 5th Way
list.forEach(Demo2::evenOdd);
}
}