Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
615 lines (462 loc) · 15.2 KB

File metadata and controls

615 lines (462 loc) · 15.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Demonstrate ArrayList.
import java.util.*;
class ArrayListDemo {
public static void main(String[] args) {
// Create an array list.
ArrayList<Character> al = new ArrayList<Character>();
System.out.println("Initial size: " + al.size());
// Add elements to the end of the list one at a time.
al.add('A');
al.add('B');
al.add('C');
al.add('D');
al.add('E');
System.out.println("\nSize after additions: " + al.size());
// Display the array list using its toString() representation.
System.out.println("Contents: " + al);
// Now, add elements to middle of the list.
// This will cause the array to expand.
for(int i = 0; i < 3; i++)
al.add(2, (char) ('x' + i));
System.out.println("\nSize after additions: " + al.size());
System.out.println("Contents: " + al);
// Now, delete the elements just added.
// This will cause the array to contract.
for(int i = 0; i < 3; i++)
al.remove(2);
System.out.println("\nSize after deletions: " + al.size());
System.out.println("Contents: " + al);
// Use set() to set the value at an index.
for(int i=0; i < al.size(); i++)
al.set(i, Character.toLowerCase(al.get(i)));
System.out.println("\nAfter changing to lowercase.");
System.out.println("Contents: " + al);
// Find and remove a value
int idx = al.indexOf('d');
if(idx >= 0) al.remove(idx);
System.out.println("\nAfter finding and removing d.");
System.out.println("Contents: " + al);
// Empty the list.
al.clear();
System.out.println("\nAfter clearing the list.");
System.out.println("Contents: " + al);
// Add the digits 0 through 9
for(int i=0; i < 10; i++)
al.add((char) ('0' + i));
// Display every other element.
System.out.print("\nHere is every other digit: ");
for(int i=0; i < al.size(); i+=2)
System.out.print(al.get(i) + " ");
}
}
// -----------------------------------------
// Demonstrate LinkedList.
import java.util.*;
class LinkedListDemo {
public static void main(String[] args) {
// Create a linked list.
LinkedList<Character> ll = new LinkedList<Character>();
// Add elements to the linked list.
ll.add('B');
ll.add('E');
ll.add('F');
System.out.println("Original contents: " + ll);
// Demonstrate addLast() and addFirst().
ll.addLast('G');
ll.addFirst('A');
System.out.println("\nAfter calls to addFirst() and addLast().");
System.out.println("Contents: " + ll);
// Add elements at an index.
ll.add(2, 'D');
ll.add(2, 'C');
System.out.println("\nAfter insertions.");
System.out.println("Contents: " + ll);
// Display first and last elements.
System.out.println("\nHere are the first and last elements: " +
ll.getFirst() + " " + ll.getLast());
// Create a sublist view.
List<Character> sub = ll.subList(2, 5);
System.out.println("\nContents of sublist view: " + sub);
// Create a new list that contains the sublist
LinkedList<Character> ll2 = new LinkedList<Character>(sub);
// Remove the elements in ll2 from ll.
ll.removeAll(ll2);
System.out.println("\nAfter removing ll2 from ll.");
System.out.println("Contents: " + ll);
// Remove first and last elements.
ll.removeFirst();
ll.removeLast();
System.out.println("\nAfter deleting first and last element: ");
System.out.println("Contents: " + ll);
// Get and set a value through an index.
ll.set(0, Character.toLowerCase(ll.get(0)));
System.out.println("\nAfter change: " + ll);
}
}
// -----------------------------------------
import java.util.*;
public class ViewAndArrayDemo {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
String[] array = list.toArray(new String[4]);
List<String> sublist = list.subList(0,4);
array[1] = "F";
sublist.set(2,"O");
System.out.println(list);
}
}
// -----------------------------------------
// Demonstrate HashSet.
import java.util.*;
class HashSetDemo {
public static void main(String[] args) {
// Create a hash set.
HashSet<Character> hs = new HashSet<Character>();
// Add elements to the hash set.
hs.add('A');
hs.add('B');
hs.add('C');
hs.add('D');
System.out.println("Original contents: " + hs);
// Add more elements.
hs.add('E');
hs.add('F');
hs.add('G');
hs.add('H');
System.out.println("\nContents after additions: " + hs);
// Delete E and H.
hs.remove('E');
hs.remove('H');
System.out.println("\nContents after deleting E and H: " + hs);
// Add E back in.
hs.add('E');
System.out.println("\nContents after adding E: " + hs);
// Add a collection of elements to hash set.
ArrayList<Character> al = new ArrayList<Character>();
al.add('X');
al.add('Y');
al.add('Z');
hs.addAll(al);
System.out.println("\nContents after adding collection: " + hs);
}
}
// -----------------------------------------
// A TreeSet creates a sorted tree.
import java.util.*;
class TreeSetDemo {
public static void main(String[] args) {
char[] chrs = { 'V', 'J', 'L', 'E', 'T', 'Q', 'C', 'P' };
// Create a tree set and a hash set.
TreeSet<Character> ts = new TreeSet<Character>();
HashSet<Character> hs = new HashSet<Character>();
System.out.print("Contents of chrs: ");
for(char ch : chrs)
System.out.print(ch + " ");
System.out.println();
// First, add the characters to the hash set.
for(char ch : chrs)
hs.add(ch);
System.out.println("Contents of hash set: " + hs);
// Next, add the characters to the tree set.
for(char ch : chrs)
ts.add(ch);
System.out.println("Contents of tree set: " + ts);
}
}
// -----------------------------------------
// LinkedHashSet compared with HashSet and TreeSet.
import java.util.*;
class SetsDemo {
public static void main(String[] args) {
char[] chrs = { 'V', 'J', 'L', 'E', 'T', 'Q', 'C', 'P' };
// Create all three sets.
TreeSet<Character> ts = new TreeSet<Character>();
HashSet<Character> hs = new HashSet<Character>();
LinkedHashSet<Character> lhs = new LinkedHashSet<Character>();
System.out.print("Contents of chrs: ");
for(char ch : chrs)
System.out.print(ch + " ");
System.out.println();
// First, add the characters to the hash set.
for(char ch : chrs)
hs.add(ch);
System.out.println("Contents of hash set: " + hs);
// Next, add the characters to the tree set.
for(char ch : chrs)
ts.add(ch);
System.out.println("Contents of tree set: " + ts);
// Finally, add the characters to the linked hash set.
for(char ch : chrs)
lhs.add(ch);
System.out.println("Contents of linked hash set: " + lhs);
}
}
// -----------------------------------------
// Demonstrate ArrayDeque.
// First the use the deque as as a stack.
// Then, use it as a FIFO queue.
import java.util.*;
class ArrayDequeDemo {
public static void main(String[] args) {
// Create an array deque.
ArrayDeque<Character> adq = new ArrayDeque<Character>();
System.out.println("Using adq as a stack.");
// Use adq like a stack.
System.out.print("Pushing: ");
// push items on the stack
for(char ch = 'A'; ch <= 'Z'; ch++) {
adq.push(ch);
System.out.print(ch);
}
System.out.println();
// now, pop them off
System.out.print("Popping: ");
while(adq.peek() != null)
System.out.print(adq.pop());
System.out.println("\n");
System.out.println("Using adq as a FIFO queue.");
// Now, use adq as a FIFO queue.
System.out.print("Queueing: ");
for(char ch = 'A'; ch <= 'Z'; ch++) {
adq.offerLast(ch);
System.out.print(ch);
}
System.out.println();
// now, remove them
System.out.print("Removing: ");
while(adq.peek() != null)
System.out.print(adq.pollFirst());
}
}
// -----------------------------------------
// Demonstrate both Iterator and ListIterator.
import java.util.*;
class IteratorDemo {
public static void main(String[] args) {
// Create a list of strings.
ArrayList<String> al = new ArrayList<String>();
// Add entries to the array list.
al.add("Alpha");
al.add("Beta");
al.add("Gamma");
al.add("Delta");
al.add("Epsilon");
al.add("Zeta");
al.add("Eta");
// First, use Iterator.
// Use Iterator to display the contents of the list.
System.out.print("Original contents: ");
Iterator<String> itr = al.iterator();
while(itr.hasNext())
System.out.print(itr.next() + " ");
System.out.println();
// Use Iterator to remove Gamma from the list.
itr = al.iterator();
while(itr.hasNext()) {
if(itr.next().equals("Gamma"))
itr.remove();
}
System.out.print("Contents after deletion: ");
itr = al.iterator();
while(itr.hasNext())
System.out.print(itr.next() + " ");
System.out.println();
// Now, use ListIterator.
// Use ListIterator to add Gamma back to the list.
ListIterator<String> litr = al.listIterator();
while(litr.hasNext()) {
if(litr.next().equals("Beta"))
litr.add("Gamma");
}
System.out.print("Contents after addition: ");
litr = al.listIterator();
while(litr.hasNext())
System.out.print(litr.next() + " ");
System.out.println();
// Use ListIterator to modify the objects being iterated.
String str;
litr = al.listIterator();
while(litr.hasNext()) {
str = litr.next();
if(str.equals("Eta"))
litr.set("Omega");
else if(str.equals("Zeta"))
litr.set("Psi");
else if(str.equals("Epsilon"))
litr.set("Chi");
else if(str.equals("Delta"))
litr.set("...");
}
System.out.print("Contents after changes: ");
litr = al.listIterator();
while(litr.hasNext())
System.out.print(litr.next() + " ");
System.out.println();
// Use ListIterator to display the list backwards.
System.out.print("Modified list backwards: ");
while(litr.hasPrevious()) {
System.out.print(litr.previous() + " ");
}
System.out.println();
}
}
// -----------------------------------------
import java.util.*;
class HashMapDemo {
public static void main(String[] args) {
// Create a hash map.
HashMap<String, Double> hm = new HashMap<String, Double>();
// Put elements to the map
hm.put("John Doe", 3434.34);
hm.put("Tom Smith", 123.22);
hm.put("Jane Baker", 1378.00);
hm.put("Todd Hall", 99.22);
hm.put("Ralph Smith", -19.08);
// Get a set of the entries.
Set<Map.Entry<String, Double>> set = hm.entrySet();
// Display the set.
for(Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account.
double balance = hm.get("John Doe");
hm.put("John Doe", balance + 1000);
System.out.println("John Doe's new balance: " +
hm.get("John Doe"));
}
}
// -----------------------------------------
import java.util.*;
class TreeMapDemo {
public static void main(String[] args) {
// Create a tree map.
TreeMap<String, Double> tm = new TreeMap<String, Double>();
// Put elements to the map.
tm.put("John Doe", 3434.34);
tm.put("Tom Smith", 123.22);
tm.put("Jane Baker", 1378.00);
tm.put("Todd Hall", 99.22);
tm.put("Ralph Smith", -19.08);
// Get a set of the entries.
Set<Map.Entry<String, Double>> set = tm.entrySet();
// Display the elements.
for(Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account.
double balance = tm.get("John Doe");
tm.put("John Doe", balance + 1000);
System.out.println("John Doe's new balance: " +
tm.get("John Doe"));
}
}
// -----------------------------------------
// Use a comparator to sort accounts by last name.
import java.util.*;
// Compare last whole words in two strings.
class NameComp implements Comparator<String> {
public int compare(String aStr, String bStr) {
int i, j, k;
// Find index of beginning of last name.
i = aStr.lastIndexOf(' ');
j = bStr.lastIndexOf(' ');
k = aStr.substring(i).compareTo(bStr.substring(j));
if(k==0) // last names match, check entire name
return aStr.compareTo(bStr);
else
return k;
}
// No need to override equals.
}
class TreeMapDemo2 {
public static void main(String[] args) {
// Create a tree map that uses the specified comparator.
TreeMap<String, Double> tm = new TreeMap<String,
Double>(new NameComp());
// Put elements to the map.
tm.put("John Doe", 3434.34);
tm.put("Tom Smith", 123.22);
tm.put("Jane Baker", 1378.00);
tm.put("Todd Hall", 99.22);
tm.put("Ralph Smith", -19.08);
// Get a set of the entries.
Set<Map.Entry<String, Double>> set = tm.entrySet();
// Display the elements.
for(Map.Entry<String, Double> me : set) {
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
// Deposit 1000 into John Doe's account.
double balance = tm.get("John Doe");
tm.put("John Doe", balance + 1000);
System.out.println("John Doe's new balance: " +
tm.get("John Doe"));
}
}
// -----------------------------------------
// Demonstrate several algorithms.
import java.util.*;
class AlgorithmsDemo {
public static void main(String[] args) {
// Create a linked list.
LinkedList<Character> ll = new LinkedList<Character>();
// Put items in the list.
for(int i = 0; i < 26; i+=2) {
ll.add((char) ('A' + i));
ll.add((char) ('Z' - i));
}
// Display original list.
System.out.print("Original list: ");
for(char ch : ll)
System.out.print(ch);
System.out.println();
// Sort the list.
Collections.sort(ll);
System.out.print("List sorted: ");
for(char ch : ll)
System.out.print(ch);
System.out.println("\n");
// Search the list.
System.out.println("Using binarySearch() to find X.");
int i = Collections.binarySearch(ll, 'X');
if(i >= 0)
System.out.println("X found. Index is " + i);
System.out.println();
// Reverse the list.
Collections.reverse(ll);
System.out.print("List reversed: ");
for(char ch : ll)
System.out.print(ch);
System.out.println("\n");
// Rotate the List.
Collections.rotate(ll, 5);
System.out.print("List rotated: ");
for(char ch : ll)
System.out.print(ch);
System.out.println("\n");
// Create a new list.
ll = new LinkedList<Character>();
// Add a string to it.
String str = "this is a test";
for(char ch : str.toCharArray())
ll.add(ch);
System.out.print("Here is the new list: ");
for(char ch : ll)
System.out.print(ch);
System.out.println();
// Replace all t's with *
Collections.replaceAll(ll, 't', '*');
System.out.print("After replacements: ");
for(char ch : ll)
System.out.print(ch);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.