Recall the IntegerList ADT whose interface is reproduced below. (Comments are in
ID: 3711327 • Letter: R
Question
Recall the IntegerList ADT whose interface is reproduced below. (Comments are included to remind you what each method does.) public interface IntegerListinterface public boolean isEmpty: 17 return true iff list has no elements public int size ) public int get (int index)7 return list element at index public void add (int index, int newItem): 17 insert newItem at index public void remove (int index); 1/ delete element at index public void removeAll 0: 1/ reset list to empty state // return number of elements in list 1. Write a static void method called swap with the following heading that interchanges the items currently in positions i and j of the list. Use only the ADT operations above to do this. static void swap (IntegerList list, int i, int j)Explanation / Answer
static void swap(IntegerListInterface list, int i, int j) { int value = list.get(i); list.remove(i); list.add(i, list.get(j)); list.remove(j); list.add(j, value); }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.