Answer:
Answer a)
public void swapPairs(ArrayList<String> list) {
for(int i = 0; i <= list.size() - 2; i += 2) {
String val= list.get(i + 1);
list.set(i + 1, list.get(i));
list.set(i, val); } }
Answer b)
Using the Iterator interface to remove the strings of even length from the list:
public void removeEvenLength( ArrayList<String> list) {
Iterator<String> iter= list.iterator();
while( iter.hasNext() ) {
String str= iter.next();
if( str.length() % 2 == 0 ) {
iter.remove(); } } }
Not using Iterator interface:
public static void removeEvenLength(ArrayList<String> list) {
for (int i = 0; i < list.size(); i++) {
String str= list.get(i);
if (str.length() % 2 == 0) {
list.remove(i);
i--; } } }
Answer c)
public static void removeDuplicates(ArrayList<String> list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).equals(list.get(i + 1))) {
list.remove(i + 1);
i--; } } }
Step-by-step explanation:
a) The method swapPairs(ArrayList<String> list) ArrayList is used to store elements of String type. The for loop has a variable i that is initialized by 0. It moves through the list. This loop's body stops executing when the value of i exceeds the size-2 of the list. The size() method returns the size of the list. This means i stops moving through the list after the last pair of values is reached. In the loop body, get(i+1) method is used to obtain the element in the list at a index i+1. Lets say in the list {"four", "score", "and", "seven", "years", "ago"}, if i is at element "four" then i+1 is at "score". Then "score" is stored in val variable. Then set() method is used to set i-th element in an ArrayList object at the i+1 index position. The second set(i,val) method is used to set the value in val variable to i-th position/index. So this is how score and four are switched.
b) The method removeEvenLength( ArrayList<String> list) takes ArrayList of Strings as parameter. Iterator is an interface which is used to traverse or iterate through object elements and here it is used to remove the elements. The iterator() method is used to traverse through the array list. The while loop will keep executing till all the elements of the ArrayList are traversed. hasNext() method is used to return True if more elements in the ArrayList are to be traversed. next() method is used to return next element in the ArrayList and store that element in str variable. Then the if statement is used to check if the element in str variable is of even length. The length() function returns the length of the element in ArrayList and modulus is used to check if the length of the element is even. If the condition evaluates to true then the remove() method is used to remove that element of even length from the list.
c) The method removeDuplicates(ArrayList<String> list) takes ArrayList of Strings as parameter and eliminates any duplicates from the list. It has a for loop that moves through the list and it stops executing when the value of i exceeds the size - 1 of the list. The if condition has two method i.e. get() and equals(). The get() method is used to obtain the element at the specified index position and equals() function is used to compare the two string elements. Here these two strings are obtained using get() method. get(i) gets element at i-th index and get(i+1) gets element at i + 1 position of the list. If both these elements are equal, then the element at i+1 index is removed. This is how all duplicates will be removed.