Question: Problem 1 (Generics) Revise method removeDuplicates, so that is a generic version and can handle the code highlighted in BOLD. Attach your .java file with

Problem 1 (Generics) Revise method removeDuplicates, so that is a generic version and can handle the code highlighted in BOLD. Attach your .java file with the entire class Problem_1B and the updated version of the method.

import java.util.ArrayList;

import java.util.Arrays;

public class Problem_1B {

public static void main(String[] args) {

ArrayList list = new ArrayList();

list.add(14);

list.add(24);

list.add(14);

ArrayList newList = removeDuplicates(list);

System.out.print(newList);

System.out.println();

String[] names = {"bob", "susan", "bob"};

ArrayList stringList = new ArrayList<>(Arrays.asList(names));

stringList = removeDuplicatesGeneric(stringList);

System.out.print(stringList);

System.out.println();

}

public static ArrayList removeDuplicates(ArrayList list){

ArrayList result = new ArrayList();

for (Integer item: list) {

if (!result.contains(item))

result.add(item);

}

return result;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!