-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayComparator.java
More file actions
59 lines (56 loc) · 1.84 KB
/
ArrayComparator.java
File metadata and controls
59 lines (56 loc) · 1.84 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
package Framework;
import java.util.Arrays;
import java.util.Comparator;
class Comp implements Comparator<Integer>{
@Override
public int compare(Integer i, Integer j){
if(i<j)
return 1;
else if(i>j)
return -1;
return 0;
}
}
public class ArrayComparator {
public static void main(String[] args) {
int[] A = {1,2,2,3,-6,9,7,4,5,-4,0};
int[] B = {1,2,2,3,-6,9,7,4,5,-4,0};
// Compare element by element
// if A > B -> 1
// if A = B -> 0
// if A < B -> -1
System.out.println(Arrays.compare(A,B));
int[] C = {1,2,2,3,-6,9,7,4,5,-4,4,3,0};
int[] D = {1,2,2,3,-6,9,7,4,5,-4,5};
System.out.println(Arrays.compare(C,D));
int[] E = {1,2,2,3,-6,9,7,4,5,-4,6,9,23};
int[] F = {1,2,2,3,-6,9,7,4,5,-4,5};
System.out.println(Arrays.compare(E,F));
int[] G = Arrays.copyOf(E,E.length);
System.out.print("Array G: ");
for (int x:G) {
System.out.print(x+" ");
}
System.out.println();
System.out.println(Arrays.equals(G,E));
System.out.println(Arrays.compare(G,E));
// Arrays.fill(G,0); // All filled with 0
// Arrays.parallelSort(G); // will sort G
Arrays.sort(G); // Same working
// Pre defined class Comparable will be used
System.out.print("Array G: ");
for (int x:G) {
System.out.print(x+" ");
}
System.out.println();
System.out.println("Index of 9: "+Arrays.binarySearch(G,23));
Integer[] H = {8,-6,5,658,-8,6,5,-5,65,50};
Arrays.sort(H,new Comp());
// User Defined Class Comp will be used for sorting
System.out.print("Array H: ");
for(int x: H){
System.out.print(x+" ");
}
System.out.println();
}
}