public class ListUtility<T>{ public List<T> findUnion(List<T> list1, List<T> lis
ID: 3592535 • Letter: P
Question
public class ListUtility<T>{
public List<T> findUnion(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
for (T item: list1)
if (!newList.contains(item))
newList.add(item);
for (T item: list2)
if (!newList.contains(item))
newList.add(item);
return newList;
}
public List<T> findIntersection(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
for (T item1: list1)
for (T item2: list2)
if (item1.equals(item2) && !newList.contains(item1))
newList.add(item1);
return newList;
}
public List<T> interleave(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
int size = list1.size() + list2.size();
for (int i = 0; i < size; i++)
if (i%2 == 0)
try {
newList.add(list1.get(i/2));
} catch (IndexOutOfBoundsException ex) {
size++;
}
else
try {
newList.add(list2.get(i/2));
} catch (IndexOutOfBoundsException ex) {
size++;
}
return newList;
}
public List<T> chopSkip (List<T> list1) {
List<T> newList = new ArrayList<>();
for (int i = 0; i < list1.size(); i = i + 2)
newList.add(list1.get(i));
return newList;
}
}
The ListUtility class is in here, does anyone help me write a test Demo(Driverclass)?
Exercise 2: Design a class called ListUtility.java that contains a list of static methods that manipulate unordered lists. public class ListUtility public static Lis findUnion(List listl, List list2)( //Create and return a third list that contains //the items that are either in listl or in list2 or both. //Do not add duplicates. public static List findIntersection(List listl, Listlist2) //Create and return a third list that contains the items /Ithat are common to both listl and list2 //Do not add duplicates. public static List interleave(List listl, List list2) //create and return a third list that contains the items //in listl interleaved with the items in list2 //For example, listl (A, C>, list2 (B, P, M, N, 2) //list3A, B, C, P, M, N, 2) public static List chopSkip(List listl)( //Create and return a list that has the items in listl //with every second item removed. //For example, if listl-A, B, C, D, E) /Ithe list returned is (A, C, EExplanation / Answer
public class ListUtility<T>{
public List<T> findUnion(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
for (T item: list1)
if (!newList.contains(item))
newList.add(item);
for (T item: list2)
if (!newList.contains(item))
newList.add(item);
return newList;
}
public List<T> findIntersection(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
for (T item1: list1)
for (T item2: list2)
if (item1.equals(item2) && !newList.contains(item1))
newList.add(item1);
return newList;
}
public List<T> interleave(List<T> list1, List<T> list2) {
List<T> newList = new ArrayList<>();
int size = list1.size() + list2.size();
for (int i = 0; i < size; i++)
if (i%2 == 0)
try {
newList.add(list1.get(i/2));
} catch (IndexOutOfBoundsException ex) {
size++;
}
else
try {
newList.add(list2.get(i/2));
} catch (IndexOutOfBoundsException ex) {
size++;
}
return newList;
}
public List<T> chopSkip (List<T> list1) {
List<T> newList = new ArrayList<>();
for (int i = 0; i < list1.size(); i = i + 2)
newList.add(list1.get(i));
return newList;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.