public static void swap(ADTList list, int index1, int index2) throws ADTOutOfBoundsException { if (index1 < 0 || index2 < 0 || index1 >= list.size() || index2 >= list.size()) throw new ADTOutOfBoundsException(index1 + " " + index2); list.add(index1, list.get(index2)); Object temp = list.get(index1 + 1); list.remove(index1 + 1); list.add(index2, (String) temp); list.remove(index2 + 1); }
public static void main(String[] args) { ADTList grocries = new ADTList(); grocries.add(0, "Butter"); grocries.add(1, "Eggs"); grocries.add(0, "Cerial"); grocries.add(1, "Milk"); grocries.add(0, "Coffee"); grocries.add(0, "Bread"); grocries.printList(); grocries.remove(2); swap(grocries, 2, 4); swap(grocries, 3, 1); grocries.printList(); swap(grocries, -1, 4); }