Repository navigation
Expand file tree
/
Copy pathArraysAndCollectionTasks.java
More file actions
89 lines (89 loc) · 6 KB
/
Copy pathArraysAndCollectionTasks.java
File metadata and controls
89 lines (89 loc) · 6 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
//import java.util.*;
//
//public class ArraysAndCollectionTasks {
//
// public static void main(String[] args) {
// // Question 1: Sorting an Array of Integers
// int[] array1 = {3, 1, 4, 1, 5, 9, 2, 6};
// Arrays.sort(array1); // Sorting student scores in ascending order to determine their ranking.
// System.out.println("Sorted Array: " + Arrays.toString(array1));
//
// // Question 2: Checking for Equality of Two Arrays
// int[] array2 = {1, 2, 3};
// boolean equal = Arrays.equals(array1, array2); // Verifying if two sets of user-selected preferences are identical to avoid redundant processing.
// System.out.println("Are arrays equal? " + equal);
//
// // Question 3: Finding the Maximum Element in an ArrayList
// ArrayList<Integer> list = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6));
// int max = Collections.max(list); // Determining the highest temperature recorded in a week to provide a weather summary.
// System.out.println("Maximum element in ArrayList: " + max);
//
// // Question 4: Shuffling Elements of an ArrayList
// Collections.shuffle(list); // Randomizing the order of multiple-choice questions in an online quiz to prevent cheating.
// System.out.println("Shuffled ArrayList: " + list);
//
// // Question 5: Finding the Frequency of an Element in an ArrayList
// int frequency = Collections.frequency(list, 1); // Analyzing customer purchase history to identify frequently bought items for targeted marketing.
// System.out.println("Frequency of '1' in ArrayList: " + frequency);
//
// // Question 6: Checking if an ArrayList is Empty
// boolean isEmpty = list.isEmpty(); // Validating if a shopping cart is empty before allowing the checkout process.
// System.out.println("Is ArrayList empty? " + isEmpty);
//
// // Question 7: Converting Array to ArrayList
// String[] array3 = {"apple", "banana", "orange"};
// ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(array3)); // Transforming an array of user input into a dynamic list for further manipulation in a contact management application.
// System.out.println("ArrayList from Array: " + arrayList);
//
// // Question 8: Reversing an Array
// Collections.reverse(Arrays.asList(array3)); // Displaying historical transaction records in reverse chronological order for financial analysis.
// System.out.println("Reversed Array: " + Arrays.toString(array3));
//
// // Question 9: Removing All Occurrences of an Element from an ArrayList
// list.removeAll(Collections.singleton(1)); // Filtering spam emails by removing blacklisted words from an inbox.
// System.out.println("ArrayList after removing all occurrences of '1': " + list);
//
// // Question 10: Copying Elements from ArrayList to Array
// Integer[] array4 = list.toArray(new Integer[0]); // Exporting a list of contact names from an address book to a legacy system that only accepts arrays.
// System.out.println("Copied Array: " + Arrays.toString(array4));
//
// // Question 11: Removing Null Values from an ArrayList
// ArrayList<String> stringList = new ArrayList<>(Arrays.asList("apple", null, "banana", null, "orange"));
// stringList.removeAll(Collections.singleton(null)); // Cleaning up a dataset by eliminating records with missing or null values before performing statistical analysis.
// System.out.println("List after removing null values: " + stringList);
//
// // Question 12: Finding the Average of an Array
// int[] numbers = {1, 2, 3, 4, 5};
// double average = Arrays.stream(numbers).average().orElse(Double.NaN); // Calculating the average daily traffic volume on a particular road segment to optimize traffic flow.
// System.out.println("Average of the array: " + average);
//
// // Question 13: Checking for Palindromes in an ArrayList
// ArrayList<String> wordList = new ArrayList<>(Arrays.asList("radar", "apple", "level", "banana"));
// for (String word : wordList) {
// boolean isPalindrome = new StringBuilder(word).reverse().toString().equalsIgnoreCase(word); // Validating if a list of passwords generated by a password manager contains any palindromes to improve security.
// System.out.println(word + " is a palindrome: " + isPalindrome);
// }
// Collections.reverse();
//
// // Question 14: Sorting a List of Strings in Reverse Order
// List<String> strList = new ArrayList<>(Arrays.asList("apple", "banana", "orange", "grape"));
// Collections.sort(strList, Collections.reverseOrder()); // Sorting a list of employee names in reverse alphabetical order to generate a descending employee directory.
// System.out.println("List of strings sorted in reverse order: " + strList);
//
// // Question 15: Finding the Index of the Last Occurrence of an Element in a List
// int lastIndex = Collections.lastIndexOfSubList(strList, Arrays.asList("apple", "banana")); // Locating the most recent entry of a particular product in an inventory management system.
// System.out.println("Index of the last occurrence of sublist: " + lastIndex);
//
// Collections.
// // Question 16: Creating an Unmodifiable List
// List<Integer> unmodifiableList = Collections.unmodifiableList(Arrays.asList(1, 2, 3, 4, 5));
// System.out.println("Unmodifiable List: " + unmodifiableList); // Providing a read-only view of a list of product features to users in a software documentation.
//
// // Question 17: Creating a Synchronized List
// List<String> synchronizedList = Collections.synchronizedList(new ArrayList<>());
// synchronizedList.add("one");
// synchronizedList.add("two");
// synchronizedList.add("three");
// System.out.println("Synchronized List: " + synchronizedList); // Managing access to a shared list of tasks among multiple threads in a multi-user task management system.
// }
//}