I have a method for sorting and removing repetitive items from an array:
public ArrayList<Integer> sortArray(ArrayList<Integer> listForSort) {
List<Integer> sortTemp = new ArrayList<Integer>();
ArrayList<Integer> Sortedlist = new ArrayList<Integer>();
int[] array = new int[20];
for (int i = 0; i < listForSort.size(); i++) {
array[i] = listForSort.get(i);
}
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
if (!(Arrays.asList(sortTemp).contains((Integer)array[i])) && (array[i] != 0)) {
Integer tempo = (Integer)array[i];
Sortedlist.add(tempo);
sortTemp.add(tempo);
}
}
return Sortedlist;
}
But my method doesn’t remove the repeated items. What is wrong?
My suggestion is to use a Set. A Set does not allow a duplicate to be entered so you wont have to worry about removing them. Maybe like a SortedSet so the sorting and duplicate removal is automatic. ConcurrentSkipListSet, NavigableSet, TreeSet are all also SortedSet’s.