-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListServices.java
More file actions
149 lines (132 loc) · 5.1 KB
/
ArrayListServices.java
File metadata and controls
149 lines (132 loc) · 5.1 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
package app;
import java.util.ArrayList;
/**
* This class provides methods that perform operations on a list of generic objects.
* The Objects are assumed to have an appropriate implementation of the equals method.
*/
public class ArrayListServices <T> {
/**
* This method takes an ArrayList as a parameter and returns a new ArrayList that
* does not contain any duplicate data. For example, if this list was passed in:
* [A, B, D, A, A, E, B], the method would return a list containing: [A, B, D, E].
* The ordering of the data does not matter.
* Assume that the parameter is not null, but it may be an empty ArrayList, in which case
* your method returns a new, empty ArrayList.
* Also note that the ArrayList that is returned must be a new ArrayList which is not the
* same as the ArrayList passed in. In other words, the parameter must not be changed by your method code.
*/
public ArrayList<T> deDuplicate(ArrayList<T> inputList){
// TODO: Implement this method.
ArrayList<T> outputList = new ArrayList<T>();
int len = inputList.size();
int count=0;
if(len>0){
outputList.add(inputList.get(0));
for(int i=1;i<len;i++){
for(int j=i-1;j>=0;j--){
if(inputList.get(j).equals(inputList.get(i))){
count++;
break;
}
}
if(count>0){
count=0;
continue;
}
else{
outputList.add(inputList.get(i));
}
}
}
return outputList;
}
/**
* This method takes two ArrayLists as parameters and returns a new ArrayList that
* contains the intersection of the data in the ArrayLists passed in. The intersection
* contains the elements that occur in both lists.
* For example, if these lists were passed in: [A, B, D, A, A, E, B], [B, E, C], the method
* would return a list containing: [B, E]. The ordering of the data does not matter. Note that
* the result does not contain any duplicates.
* Assume that the parameters are not null, but one or both may be an empty ArrayList, in which
* case your method returns a new, empty ArrayList.
* Also note that the ArrayList that is returned must be a new ArrayList which is not the same as
* the ArrayList passed in. In other words, the parameter must not be changed by your method code.
*/
public ArrayList<T> getSetIntersection(ArrayList<T> listA, ArrayList<T> listB){
// TODO: Implement this method.
ArrayList<T> listC= new ArrayList<T>();
int len1= listA.size();
int len2= listB.size();
if(len1>0 && len2>0){
for(T t1:listA){
for(T t2:listB){
if(t1.equals(t2)){
listC.add(t1);
}
}
}
listC= this.deDuplicate(listC);
}
return listC;
}
/**
* This method takes two ArrayLists as parameters and returns a new ArrayList that
* contains the set difference of the data in the ArrayLists passed in. The set difference
* contains the elements that occur only in one or the other list, but not in both.
* For example, if these lists were passed in: [A, B, D, A, A, E, B], [B, E, C], the method
* would return a list containing: [A, C]. The ordering of the data does not matter. Note
* that the result does not contain any duplicates.
* Assume that the parameters are not null, but one or both may be an empty ArrayList. In the
* case where one list is empty, your method returns a new ArrayList that contains all of the
* elements on the other list- with no duplicates. In the case where both lists are empty, your
* method returns a new, empty ArrayList.
* Also note that the ArrayList that is returned must be a new ArrayList which is not the same
* as the ArrayList passed in. In other words, the parameter must not be changed by your method code.
*/
public ArrayList<T> getSetDifference(ArrayList<T> listA, ArrayList<T> listB){
// TODO: Implement this method.
ArrayList<T> listC= new ArrayList<T>();
ArrayList<T> intersection = new ArrayList<T>();
intersection= this.getSetIntersection(listA, listB);
int len1= listA.size();
int len2=listB.size();
int count1=0,count2=0;
if(len1==0&& len2==0){
return listC;
}
else{
for( T t1:listA){
for(T t2:intersection){
if(t1.equals(t2)){
count1++;
break;
}
}
if(count1>0){
count1=0;
continue;
}
else{
listC.add(t1);
}
}
for( T t3:listB){
for(T t4:intersection){
if(t3.equals(t4)){
count2++;
break;
}
}
if(count2>0){
count2=0;
continue;
}
else{
listC.add(t3);
}
}
listC=this.deDuplicate(listC);
return listC;
}
}
}