I was asked to write a small program to remove the duplicates from a list and make a new list without the duplicates. We had to do this using Generics in Java. This is what I have so far:
All help is greatly appreciated!!!
import java.util.ArrayList;
public class Assignment13 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("one");
list.add("two");
list.add("three");
list.add("three");
System.out.println("Prior to removal: " + list);
System.out.println("after: " + list2);
}
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> list2 = new ArrayList<E>();
for (int i = 1; i < list2.size(); i++) {
String a1 = list2.get(i);
String a2 = list2.get(i-1);
if (a1.equals(a2)) {
list2.remove(a1);
}
}
return list2;
}
}