-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx18ArrayList.java
More file actions
136 lines (115 loc) · 5.13 KB
/
Ex18ArrayList.java
File metadata and controls
136 lines (115 loc) · 5.13 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package JavaEntry;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class Ex18ArrayList {
public static void main(String[] args){
// ArrayList: is a collection on element
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("Apple"); // Adding an element to the Array
fruits.add("Banana");
fruits.add("Watermelon");
fruits.add("Grapes");
fruits.add("50");
fruits.add(0, "Mango"); // Adding an element on 0 index to the Array
fruits.set(2, "Himsagar Mango"); // Replace the first element on the Array
fruits.remove(3);
// fruits.clear(); // For clear all the Array element;
System.out.println(fruits + ", Total fruits count is - " + fruits.size());
System.out.println(fruits.get(1)); // Access the element with index.
Collections.sort(fruits); // For sort the array.
// Printing all array element with loop
System.out.println("Printing all array element with For loop -");
for(int i = 0; i < fruits.size(); i++){
System.out.print(fruits.get(i) + ", ");
}
System.out.println(" ");
System.out.println("Printing with For Each -");
for(String i : fruits){
System.out.print(i + ", ");
}
// Use LinkedList to manipulate data and ArrayList for storing and accessing data.
// LinkedList: class is a collection which can contain many objects of the same type, just like the ArrayList.
System.out.println(" ");
System.out.println("Linked List -");
LinkedList<String> linkedObj = new LinkedList<String>();
linkedObj.add("7"); // Adding an element to the Linked list
linkedObj.add("1");
linkedObj.add("2");
linkedObj.addFirst("9");
linkedObj.addLast("5");
// System.out.println(linkedObj.getLast());
// Collections.sort(linkedObj); // Sorting the array
Collections.sort(linkedObj, Collections.reverseOrder()); // Reverse the array
for(String i: linkedObj){
System.out.print(i + ", ");
}
System.out.println(" ");
// HashMap: store items in "key/value" pairs, and you can access them by an index of another type (e.g. a String).
HashMap<String, String> newElem = new HashMap<String, String>();
newElem.put("a", "Apple");
newElem.put("b", "Ball");
newElem.put("c", "Cat");
System.out.println(newElem + ", Total Element count is - " + newElem.size());
// newElem.remove("b"); // For remove an element
// newElem.clear(); // For remove all element
System.out.println(newElem.get("a")); // For access element with specific key
System.out.print("Keys - ");
for(String key : newElem.keySet()){
System.out.print(key + ", ");
}
System.out.println(" ");
System.out.print("Values - ");
for(String val : newElem.values()){
System.out.print(val + ", ");
}
System.out.println(" ");
System.out.print("Key & Values - ");
for(String key : newElem.keySet()){
System.out.print(key.toUpperCase() + " for " + newElem.get(key) + ", ");
}
// HashSet is a collection of items where every item is unique
System.out.println(" ");
HashSet<Integer> alphabets = new HashSet<Integer>();
alphabets.add(1);
alphabets.add(2); // It will not add as A is already exist.
// alphabets.remove(2);
alphabets.add(3);
alphabets.add(4); // For check the element exist or not.
// alphabets.clear(); // For clear all the element.
System.out.println(alphabets.contains(3));
for(int i=0; i<10; i++){
if(!alphabets.contains(i)){
alphabets.add(i);
}
}
System.out.println(alphabets + ", Total no of element is - " + alphabets.size());
// Iterator: is an object that can be used to loop through collections.
Iterator<Integer> cusIterator = alphabets.iterator();
System.out.println(cusIterator.next() + ", " + cusIterator.next());
while (cusIterator.hasNext()) {
System.out.print(cusIterator.next() + ", ");
}
System.out.println(" ");
//Wrapper Classes : provide a way to use primitive data types (int, boolean, etc..) as objects.
// byte - Byte
// short - Short
// int - Integer
// long - Long
// float - Float
// double - Double
// boolean - Boolean
// char - Character
// Usefull because it is invalid (int not work): ArrayList<int> myNumbers = new ArrayList<int>(); // Invalid
// ArrayList<Integer> myNumbers = new ArrayList<Integer>(); // Valid
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt.toString());
System.out.println(myDouble.doubleValue());
System.out.println(myChar.charValue());
}
}