Skip to content

Commit 8e5df9c

Browse files
committed
feat(ComparatorDemo3): add demo of Arrays utilities and lexicographic comparison
What - Added ComparatorDemo3 class: - Demonstrates usage of java.util.Arrays methods for array comparison and manipulation. - Created two int[] arrays a and b. - Compared arrays with Arrays.compare(a, b) → lexicographic comparison. - Copied array a into c using Arrays.copyOf(a, a.length). - Printed all elements of c. - Converted primitive int[] to Integer[] using streams and boxing for deep comparison. - Checked equality with Arrays.deepEquals(objA, objC). - Left comments suggesting use of other methods: Arrays.mismatch(), Arrays.sort(), Arrays.parallelPrefix(). Why - Teaches how Arrays utilities simplify operations like comparison, copying, and equality checks. - Highlights difference between compare() (lexicographic) and deepEquals() (content-based). - Provides a foundation for exploring more advanced Arrays methods. How - Arrays.compare(a, b): - Compares arrays element by element. - Returns negative if first mismatching element in a < b. - Returns positive if a > b at mismatch. - Returns 0 if arrays are equal. - Arrays.copyOf(a, a.length): duplicates array a fully. - Arrays.stream(a).boxed(): converts int[] → Stream<Integer>. - toArray(Integer[]::new): collects stream into Integer[]. - Arrays.deepEquals(objA, objC): checks equality element by element for object arrays. Logic - Inputs: - a = [2,4,6,8,1,3,5,11]. - b = [2,4,6,8,1,3,5,7]. - Outputs: - Arrays.compare(a, b) → positive result (11 > 7 at mismatch). - Copy of a into c → identical to a. - Printed elements of c. - deepEquals(objA, objC) → true, since a and c are equal element by element. - Edge cases: - Arrays.compare handles different lengths: shorter is "less" if all common elements match. - deepEquals requires object arrays, hence boxing needed for int[]. - Complexity / performance: - compare() and deepEquals() run in O(n). - Boxing adds overhead; only required for object-level comparisons. - Concurrency / thread-safety: Not thread-safe; operations on local arrays are safe in demo. - Error handling: Not applicable. Real-life applications - Comparing arrays for equality or lexicographic order (e.g., version numbers). - Copying arrays for immutability or manipulation without altering originals. - Using deepEquals() for nested or object arrays. - Arrays utilities simplify common operations in algorithms, testing, or data processing. Notes - For primitive arrays, prefer Arrays.equals(a, c) instead of deepEquals after boxing. - Arrays.mismatch(a, b) can find the first differing index. - Arrays.parallelPrefix() supports efficient prefix-sum style computations in parallel. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent de566bb commit 8e5df9c

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
package ListDemo.ArraysandComparator;
2-
31
import java.util.Arrays;
42

53
/**
64
* This class demonstrates the usage of Arrays and the Comparator interface in Java.
75
*
86
* @see <a href="https://www.geeksforgeeks.org/comparator-interface-java/">Comparator Interface - GeeksforGeeks</a>
97
*/
10-
public class ComparatorDemo {
11-
public static void main(String[] args)
12-
{
8+
9+
public class ComparatorDemo3 {
10+
public static void main(String[] args) {
1311
int a[] = {2,4,6,8,1,3,5,11};
1412
int b[] = {2,4,6,8,1,3,5,7};
13+
1514
/*
16-
Checking both array same or not.
17-
Try to remove one element from both array one by one. remove one or many as you wish.
15+
Checking both arrays the same or not.
16+
Try to remove one element from both arrays one by one. remove one or many as you wish.
1817
The method Arrays.compare(a, b) in Java compares two arrays lexicographically.
1918
This means it compares elements one by one from the beginning, and as soon as it finds a mismatch,
2019
it determines the order based on that difference.
2120
*/
21+
2222
System.out.println(Arrays.compare(a,b));
2323

24-
//int c[] = Arrays.copyOf(a,4);
25-
int c[] = Arrays.copyOf(a,a.length);
26-
for(int x:c)
27-
System.out.println("Print all elements: "+x);
24+
//int c[] = Arrays.copyOf(a,4);
25+
int c[] = Arrays.copyOf(a,a.length);
26+
for(int x:c)
27+
System.out.println("Print all elements: "+x);
2828

2929
// Convert primitive int[] to Integer[] for deepEquals
3030
Integer[] objA = Arrays.stream(a).boxed().toArray(Integer[]::new);
3131
Integer[] objC = Arrays.stream(c).boxed().toArray(Integer[]::new);
3232

3333
System.out.println("Are arrays deeply equal? " + Arrays.deepEquals(objA, objC));
3434

35-
/*Try others methods also.*/
35+
/* Try others methods also. */
3636

37-
//Arrays.mismatch(, );
38-
//Arrays.sort();
39-
//Arrays.parallelPrefix();
37+
// Arrays.mismatch(, );
38+
// Arrays.sort();
39+
// Arrays.parallelPrefix();
4040
}
41-
}
41+
}

0 commit comments

Comments
 (0)