Repository navigation
Expand file tree
/
Copy pathArraysAndCollectionsDemo.java
More file actions
424 lines (346 loc) · 15 KB
/
Copy pathArraysAndCollectionsDemo.java
File metadata and controls
424 lines (346 loc) · 15 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
package basics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* ArraysAndCollectionsDemo - Demonstrates Java arrays and basic collections
*
* This class covers:
* - Array declaration, initialization, and manipulation
* - Multi-dimensional arrays
* - ArrayList, LinkedList, HashMap, HashSet
* - Basic collection operations
* - Iteration techniques
* - Common pitfalls and best practices
*
* @author Java Examples Project
* @version 1.0
*/
public class ArraysAndCollectionsDemo {
/**
* Demonstrates basic array operations
*/
public static void demonstrateArrays() {
System.out.println("=== BASIC ARRAYS ===");
// Array declaration and initialization
int[] numbers1 = new int[5]; // Creates array with default values (0)
int[] numbers2 = {1, 2, 3, 4, 5}; // Array literal
int[] numbers3 = new int[]{10, 20, 30, 40, 50}; // Explicit initialization
// Array assignment
numbers1[0] = 100;
numbers1[1] = 200;
numbers1[2] = 300;
numbers1[3] = 400;
numbers1[4] = 500;
System.out.println("Array 1: " + Arrays.toString(numbers1));
System.out.println("Array 2: " + Arrays.toString(numbers2));
System.out.println("Array 3: " + Arrays.toString(numbers3));
// Array properties and methods
System.out.println("Array length: " + numbers2.length);
// Array iteration
System.out.print("Traditional for loop: ");
for (int i = 0; i < numbers2.length; i++) {
System.out.print(numbers2[i] + " ");
}
System.out.println();
System.out.print("Enhanced for loop: ");
for (int number : numbers2) {
System.out.print(number + " ");
}
System.out.println();
// String arrays
String[] fruits = {"apple", "banana", "orange", "grape"};
System.out.println("Fruits: " + Arrays.toString(fruits));
// Array operations
Arrays.sort(fruits);
System.out.println("Sorted fruits: " + Arrays.toString(fruits));
int index = Arrays.binarySearch(fruits, "orange");
System.out.println("Index of 'orange': " + index);
// Array copying
int[] copiedArray = Arrays.copyOf(numbers2, numbers2.length);
System.out.println("Copied array: " + Arrays.toString(copiedArray));
int[] partialCopy = Arrays.copyOfRange(numbers2, 1, 4);
System.out.println("Partial copy (index 1-3): " + Arrays.toString(partialCopy));
}
/**
* Demonstrates multi-dimensional arrays
*/
public static void demonstrateMultiDimensionalArrays() {
System.out.println("\n=== MULTI-DIMENSIONAL ARRAYS ===");
// 2D array declaration and initialization
int[][] matrix1 = new int[3][4]; // 3 rows, 4 columns
int[][] matrix2 = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Filling 2D array
int value = 1;
for (int i = 0; i < matrix1.length; i++) {
for (int j = 0; j < matrix1[i].length; j++) {
matrix1[i][j] = value++;
}
}
// Printing 2D array
System.out.println("Matrix 1:");
for (int[] row : matrix1) {
System.out.println(Arrays.toString(row));
}
System.out.println("Matrix 2:");
for (int[] row : matrix2) {
System.out.println(Arrays.toString(row));
}
// Jagged arrays (arrays with different row lengths)
int[][] jaggedArray = {
{1, 2},
{3, 4, 5, 6},
{7, 8, 9}
};
System.out.println("Jagged array:");
for (int i = 0; i < jaggedArray.length; i++) {
System.out.println("Row " + i + ": " + Arrays.toString(jaggedArray[i]));
}
}
/**
* Demonstrates ArrayList operations
*/
public static void demonstrateArrayList() {
System.out.println("\n=== ARRAYLIST ===");
// ArrayList creation and initialization
ArrayList<String> cities = new ArrayList<>();
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
// Adding elements
cities.add("New York");
cities.add("London");
cities.add("Tokyo");
cities.add("Paris");
System.out.println("Cities: " + cities);
System.out.println("Numbers: " + numbers);
// Accessing elements
System.out.println("First city: " + cities.get(0));
System.out.println("Last city: " + cities.get(cities.size() - 1));
// Modifying elements
cities.set(1, "Los Angeles");
System.out.println("After modification: " + cities);
// Inserting at specific position
cities.add(2, "Berlin");
System.out.println("After insertion: " + cities);
// Removing elements
cities.remove("Tokyo");
cities.remove(0); // Remove by index
System.out.println("After removal: " + cities);
// Searching
boolean containsParis = cities.contains("Paris");
int indexOfBerlin = cities.indexOf("Berlin");
System.out.println("Contains Paris: " + containsParis);
System.out.println("Index of Berlin: " + indexOfBerlin);
// Size and empty check
System.out.println("Size: " + cities.size());
System.out.println("Is empty: " + cities.isEmpty());
// Iteration
System.out.print("Using enhanced for loop: ");
for (String city : cities) {
System.out.print(city + " ");
}
System.out.println();
// Using Iterator
System.out.print("Using Iterator: ");
Iterator<String> iterator = cities.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
}
/**
* Demonstrates HashMap operations
*/
public static void demonstrateHashMap() {
System.out.println("\n=== HASHMAP ===");
// HashMap creation and initialization
HashMap<String, Integer> ageMap = new HashMap<>();
HashMap<Integer, String> gradeMap = new HashMap<Integer, String>() {{
put(90, "A");
put(80, "B");
put(70, "C");
put(60, "D");
}};
// Adding key-value pairs
ageMap.put("Alice", 25);
ageMap.put("Bob", 30);
ageMap.put("Charlie", 35);
ageMap.put("Diana", 28);
System.out.println("Age map: " + ageMap);
System.out.println("Grade map: " + gradeMap);
// Accessing values
Integer aliceAge = ageMap.get("Alice");
String gradeFor90 = gradeMap.get(90);
System.out.println("Alice's age: " + aliceAge);
System.out.println("Grade for 90: " + gradeFor90);
// Checking existence
boolean hasCharlie = ageMap.containsKey("Charlie");
boolean hasAge25 = ageMap.containsValue(25);
System.out.println("Has Charlie: " + hasCharlie);
System.out.println("Has age 25: " + hasAge25);
// Updating values
ageMap.put("Alice", 26); // Updates existing value
System.out.println("After updating Alice's age: " + ageMap);
// Using putIfAbsent
ageMap.putIfAbsent("Eve", 22);
ageMap.putIfAbsent("Alice", 99); // Won't update since Alice exists
System.out.println("After putIfAbsent: " + ageMap);
// Removing entries
ageMap.remove("Bob");
System.out.println("After removing Bob: " + ageMap);
// Iteration through HashMap
System.out.println("Iterating through entries:");
for (Map.Entry<String, Integer> entry : ageMap.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
System.out.println("All keys: " + ageMap.keySet());
System.out.println("All values: " + ageMap.values());
}
/**
* Demonstrates HashSet operations
*/
public static void demonstrateHashSet() {
System.out.println("\n=== HASHSET ===");
// HashSet creation and initialization
HashSet<String> colors = new HashSet<>();
HashSet<Integer> numbers = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 3, 2, 1));
// Adding elements
colors.add("red");
colors.add("blue");
colors.add("green");
colors.add("red"); // Duplicate - won't be added
System.out.println("Colors: " + colors);
System.out.println("Numbers (duplicates removed): " + numbers);
// Checking existence
boolean hasBlue = colors.contains("blue");
System.out.println("Has blue: " + hasBlue);
// Removing elements
colors.remove("green");
System.out.println("After removing green: " + colors);
// Set operations
HashSet<String> primaryColors = new HashSet<>(Arrays.asList("red", "blue", "yellow"));
HashSet<String> warmColors = new HashSet<>(Arrays.asList("red", "orange", "yellow"));
// Union (using addAll)
HashSet<String> allColors = new HashSet<>(primaryColors);
allColors.addAll(warmColors);
System.out.println("Union of primary and warm colors: " + allColors);
// Intersection (using retainAll)
HashSet<String> intersection = new HashSet<>(primaryColors);
intersection.retainAll(warmColors);
System.out.println("Intersection of primary and warm colors: " + intersection);
// Difference (using removeAll)
HashSet<String> difference = new HashSet<>(primaryColors);
difference.removeAll(warmColors);
System.out.println("Primary colors not in warm colors: " + difference);
// Size and empty check
System.out.println("Size: " + colors.size());
System.out.println("Is empty: " + colors.isEmpty());
}
/**
* Demonstrates LinkedList operations
*/
public static void demonstrateLinkedList() {
System.out.println("\n=== LINKEDLIST ===");
LinkedList<String> queue = new LinkedList<>();
// Adding elements (queue operations)
queue.addFirst("First");
queue.addLast("Last");
queue.add("Middle");
queue.offer("Offered"); // Same as addLast
System.out.println("LinkedList: " + queue);
// Accessing elements
System.out.println("First element: " + queue.getFirst());
System.out.println("Last element: " + queue.getLast());
System.out.println("Peek first: " + queue.peekFirst());
// Removing elements (queue operations)
String removed = queue.removeFirst();
System.out.println("Removed first: " + removed);
System.out.println("After removal: " + queue);
String polled = queue.poll(); // Same as removeFirst
System.out.println("Polled: " + polled);
System.out.println("After polling: " + queue);
// Using as stack (LIFO)
LinkedList<Integer> stack = new LinkedList<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println("Stack: " + stack);
System.out.println("Pop: " + stack.pop());
System.out.println("Stack after pop: " + stack);
}
/**
* Demonstrates common collection operations and utilities
*/
public static void demonstrateCollectionUtilities() {
System.out.println("\n=== COLLECTION UTILITIES ===");
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 8, 1, 9, 3));
System.out.println("Original list: " + numbers);
// Sorting
Collections.sort(numbers);
System.out.println("Sorted list: " + numbers);
// Reverse
Collections.reverse(numbers);
System.out.println("Reversed list: " + numbers);
// Shuffle
Collections.shuffle(numbers);
System.out.println("Shuffled list: " + numbers);
// Min and Max
System.out.println("Min: " + Collections.min(numbers));
System.out.println("Max: " + Collections.max(numbers));
// Fill
List<String> words = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
Collections.fill(words, "filled");
System.out.println("Filled list: " + words);
// Frequency
List<String> letters = Arrays.asList("a", "b", "a", "c", "a", "b");
int frequency = Collections.frequency(letters, "a");
System.out.println("Frequency of 'a': " + frequency);
}
/**
* Main method demonstrating all array and collection concepts
* @param args command line arguments
*/
public static void main(String[] args) {
System.out.println("Java Arrays and Collections Demonstration");
System.out.println("========================================");
demonstrateArrays();
demonstrateMultiDimensionalArrays();
demonstrateArrayList();
demonstrateHashMap();
demonstrateHashSet();
demonstrateLinkedList();
demonstrateCollectionUtilities();
System.out.println("\n=== COMPARISON: ARRAYS VS COLLECTIONS ===");
System.out.println("Arrays:");
System.out.println(" + Fixed size, memory efficient");
System.out.println(" + Direct element access by index");
System.out.println(" + Can store primitives directly");
System.out.println(" - Size cannot be changed after creation");
System.out.println(" - Limited built-in methods");
System.out.println("\nCollections:");
System.out.println(" + Dynamic size");
System.out.println(" + Rich set of methods for manipulation");
System.out.println(" + Type safety with generics");
System.out.println(" - Only store objects (primitives are auto-boxed)");
System.out.println(" - Slight memory overhead");
System.out.println("\n=== WHEN TO USE WHAT ===");
System.out.println("Use Arrays when:");
System.out.println(" - Size is known and fixed");
System.out.println(" - Performance is critical");
System.out.println(" - Working with primitives heavily");
System.out.println("\nUse Collections when:");
System.out.println(" - Size may change during runtime");
System.out.println(" - Need rich manipulation methods");
System.out.println(" - Working with objects");
System.out.println(" - Need specific data structure behavior (Set, Map, Queue)");
}
}