This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ArraysLists.java
83 lines (50 loc) · 1.67 KB
/
ArraysLists.java
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
/*-- Comparing two lists' objects --*/
import java.util.*;
public class ArraysLists {
static List<Integer> compareLists(List<Integer> a, List<Integer> b) {
List<Integer> list1 = new ArrayList<Integer>();
list1 = a;
List<Integer> list2 = new ArrayList<Integer>();
list2 = b;
/* This line is tricky */
/*
Integer[] arr1 = new Integer[list1.size];
arr1 = list1.toArray();
The above two lines can be merged and can be written as:
Integer[] arr1 = list1.toArray(new Integer[list1.size()]);
*/
Integer[] arr1 = list1.toArray(new Integer[list1.size()]);
Integer[] arr2 = list2.toArray(new Integer[list2.size()]);
int aaa=0;
int bbb=0;
for(int i=0; i<list1.size(); i++){
if(arr1[i]==arr2[i])
{}
else if(arr1[i]>arr2[i])
aaa++;
else if(arr1[i]<arr2[i])
bbb++;
}
List<Integer> arrFinal = new ArrayList<Integer>();
arrFinal.add(aaa);
arrFinal.add(bbb);
return arrFinal;
}
/*-- Inputs from the main method --*/
public static void main(String[] args){
List<Integer> A = new ArrayList<>(new Integer(10));
A.add(20);
A.add(30);
A.add(40);
A.add(50);
List<Integer> B = new ArrayList<>(new Integer(30));
B.add(40);
B.add(40);
B.add(40);
B.add(40);
List<Integer> list = compareLists(A,B);
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
}
}