-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayArraylistLinklist.java
More file actions
430 lines (347 loc) · 12.7 KB
/
ArrayArraylistLinklist.java
File metadata and controls
430 lines (347 loc) · 12.7 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.net.Socket;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
class Ar1 {
public static void main(String[] args) {
String[] Playlist = new String[5];
Playlist[0] = "No lie";
Playlist[1] = "Attention";
Playlist[2] = "Despacito";
Playlist[3] = "Shape of you";
Playlist[4] = "Perfect";
for (int i=0; i <Playlist.length; i++)
{
System.out.println("Song " + i + " is: " + Playlist[i]);
}
}
}
class Ar2 {
public static void main(String[] args) {
String[] Playlist = {"No lie", "Attention", "Despacito", "Shape of you", "Perfect"};
for (int i=0; i <Playlist.length; i++)
{
System.out.println("Song " + i + " is: " + Playlist[i]);
}
}
}
class QArr1{
public static void main(String[] args) {
int [] Score = new int[5];
Scanner scan = new Scanner(System.in);
System.out.println("Enter 5 Scores: ");
Score[0] = scan.nextInt();
Score[1] = scan.nextInt();
Score[2] = scan.nextInt();
Score[3] = scan.nextInt();
Score[4] = scan.nextInt();
for (int i=0; i<Score.length; i++)
{
if(Score[i] <= 100 && Score[i] >=75)
{
System.out.println("Your Score "+Score +" is Grade A");
}
else if (Score[i] <=74 && Score[i] >=65 )
{
System.out.println("Youre Score " +Score + "is Grade B");
}
else if (Score[i] <=64 && Score[i] >=45)
{
System.out.println("Youre Score " +Score + "is Grade C");
}
else if (Score[i] <=44 && Score[i] >=35)
{
System.out.println("Youre Score " +Score + "is Grade S");
}
else
{
}
}
}
}
class mutable{
public static void main(String[] args) {
for(int i=0; i<16; i++)
{
System.out.println(i +" x 2" + i*2);
}
System.out.println("_______________________________");
}
}
// -- Array Lists--
class ArrList1{
public static void main(String[] args) {
// Set Array list Type & Name
ArrayList <String> Students = new ArrayList<>();
// add Elements
Students.add("Steve");
Students.add("Chris");
Students.add("Tony");
Students.add("Wanda");
Students.add("Ultorn");
Students.add(5,"Vision"); // Add By Index
// Display the Arraylist
System.out.println("Students of Class A : " +Students);
System.out.println();
// Access a Students
System.out.println("Students at Index 4 : " + Students.get(4));
System.out.println();
//Update a Student's Name use index
Students.set(3, "Waston");
Students.set(5, "Hakwey");
System.out.println("Updated Students List : " +Students);
System.out.println();
// Remove a Stuedent
Students.remove("Steve"); // Remove by Obj Name
Students.remove(3); // Remove by Index
System.out.println("After Removal: " + Students);
System.out.println();
//Check if a Student exists
System.out.println("Contains Waston ? " + Students.contains("Waston"));
System.out.println("Contains Steve ? " + Students.contains("Steve"));
System.out.println();
//Display Size
System.out.println("Total Students: " +Students.size());
System.out.println();
//Clear all Students
Students.clear();
System.out.println("After Clearing: " + Students);
System.out.println("Total Students After Removal : " +Students.size());
System.out.println("_______________________________");
}
}
class QueArrList1{
public static void main(String[] args) {
ArrayList <Integer> Number = new ArrayList<>();
// Add 5 int numbers
Number.add(54);
Number.add(62);
Number.add(76);
Number.add(3,41);
Number.add(4,25);
Number.add(5,50);
System.out.println("The Numbers are : " +Number);
System.out.println();
//Retrieve(get) The 3rd Element & Display it
System.out.println("The 3 Element is : " +Number.get(2));
System.out.println();
// Replace The Second Element with new value
Number.set(1, 12);
System.out.println("After Change 2 Element : " +Number);
System.out.println();
// Remove the last element
Number.remove(5);
System.out.println("After Remove Last element : " +Number);
System.out.println();
// Display Size
System.out.println("Total Numbers are " +Number.size());
System.out.println("_______________________________");
}
}
class QArrList2{
public static void main(String[] args) {
ArrayList <Integer> Score = new ArrayList<>();
Score.add(85);
Score.add(45);
Score.add(66);
Score.add(75);
Score.add(56);
for(int i=0; i<Score.size(); i++)
{
if(Score.get(i) <= 100 && Score.get(i) >=75)
{
System.out.println("Your Score "+Score.get(i) +" is Grade A");
}
else if (Score.get(i) <=74 && Score.get(i) >=65 )
{
System.out.println("Youre Score " +Score.get(i) + " is Grade B");
}
else if (Score.get(i) <=64 && Score.get(i) >=45)
{
System.out.println("Youre Score " +Score.get(i) + " is Grade C");
}
else if (Score.get(i) <=44 && Score.get(i) >=35)
{
System.out.println("Youre Score " +Score.get(i) + " is Grade S");
}
else
{
}
System.out.println("_______________________________");
}
}
}
class IterationExample{
public static void main(String[] args) {
ArrayList <Double> grades = new ArrayList<>();
grades.add(75.8);
grades.add(64.4);
grades.add(75.3);
//Using a for-each loop
for(double grade: grades)
{
System.out.println("Grade: " +grade);
}
// Using a for loop With index
for(int i=0; i<grades.size(); i++)
{
System.out.println("Grade at index " +i +" : " +grades.get(i));
}
System.out.println("_______________________________");
}
}
// Arraylist Exercises
// Ex 1 : Create ArrayList of integers, add the number 1 to 5 and print the list
class EX1Arrlist{
public static void main(String[] args) {
ArrayList <Integer> Number = new ArrayList<>();
for(int i=0; i<=5; i++)
{
Number.add(i);
}
System.out.println("Array list : " +Number);
System.out.println("_______________________________");
}
}
// EX 2 : Create an Array List of Stings add some names, remove one by its value, and print the updated list
class EX2Arrlist{
public static void main(String[] args) {
ArrayList <String> name = new ArrayList<>();
name.add("Sam Wan");
name.add("Petter Parker");
name.add("Loki Odin");
System.out.println("The Names are : " +name);
name.remove(1);
System.out.println("The Names are after removal : " +name);
System.out.println("_______________________________");
}
}
// EX 3 : Find the maximum & minimum values in ArrayList of integers
class EX3Arrlist{
public static void main(String[] args) {
ArrayList <Integer> num = new ArrayList<>();
num.add(70);
num.add(96);
num.add(21);
num.add(86);
num.add(45);
int max = Collections.max(num);
int min = Collections.min(num);
System.out.println("Maximum : " +max);
System.out.println("Minimum : " +min);
System.out.println("_______________________________");
}
}
// EX 4 : Check if a specific element exists in an ArrayList
class EX4ArrList{
public static void main(String[] args) {
ArrayList <String> fruits = new ArrayList<>();
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Graphs");
fruits.add("Mango");
String fruitsToCheck = "Apple";
if (fruits.contains(fruitsToCheck))
{
System.out.println(fruitsToCheck + " is in the list ");
}
else
{
System.out.println(fruitsToCheck+ " is not in the list");
}
System.out.println("_______________________________");
}
}
// EX 5 Reverse ArrayList with out Using Collections.reverse()
class EX5ArrList{
public static void main(String[] args) {
ArrayList <Integer> num = new ArrayList<>();
num.add(1);
num.add(2);
num.add(3);
num.add(4);
num.add(5);
ArrayList <Integer> reversed = new ArrayList<>();
for(int i=num.size() -1; i>=0; i--)
{
reversed.add(num.get(i));
}
System.out.println("Original : " +num);
System.out.println("Reversed : " +reversed);
System.out.println("_______________________________");
}
}
// EX 6 Merge two Arraylist and Remove duplicates
class EX6ArrList{
public static void main(String[] args) {
ArrayList <String> list1 = new ArrayList<>();
list1.add("Apple");
list1.add("Banana");
list1.add("Orange");
ArrayList <String> list2 = new ArrayList<>();
list2.add("Banana");
list2.add("Grapes");
list2.add("Apple");
HashSet <String> mergedSet = new HashSet<>(list1);
mergedSet.addAll(list2);
ArrayList <String> mergedList = new ArrayList<>(mergedSet);
System.out.println("Merged List : " +mergedList);
System.out.println("_______________________________");
}
}
// EX 7 Sort ArrayList in ascending & Descending order
class EX7Arrlist{
public static void main(String[] args) {
ArrayList <Integer> number = new ArrayList<>();
number.add(21);
number.add(86);
number.add(72);
number.add(32);
number.add(61);
number.add(54);
number.add(75);
// Asending order
Collections.sort(number);
System.out.println("Ascending Order : " +number);
System.out.println();
// Descending Order
Collections.sort(number, Collections.reverseOrder());
System.out.println("Descending Order : " +number);
System.out.println("_______________________________");
}
}
// --- Linked List ---
class LinkLst1{
public static void main(String[] args) {
//Creating the linklist
LinkedList <Integer> list = new LinkedList<>();
//Adding Elements
list.add(12);
list.add(34);
list.add(87);
list.add(65);
list.add(47);
System.out.println("The Elemnts in the Linklist : " +list);
System.out.println("-------------------------------");
//Traversing the list
for(int item:list)
{
System.out.println(item + "- >");
}
System.out.println("null");
System.out.println("-------------------------------");
//Removing an Element
list.remove(1);
System.out.println("List After Removal : " +list);
System.out.println("-------------------------------");
//Add Element by index
list.add(1,77);
System.out.println("List After Adding : " +list);
System.out.println("-------------------------------");
// Accessing an Elemnet
System.out.println("element at index 2 : " + list.get(2));
System.out.println("-------------------------------");
}
}