Answer:
In Java:
public static void clump(ArrayList<String> MyList) {
for(int i = 0; i<MyList.size()-1;i++){
String newStr = "(" + MyList.get(i) + " "+ MyList.get(i + 1) + ")";
MyList.set(i, newStr);
MyList.remove(i + 1);
}
System.out.println("Updated ArrayList: " + MyList);
}
Explanation:
First, we define the method
public static void clump(ArrayList<String> MyList) {
Next, we iterate through the ArrayList
for(int i = 0; i<MyList.size()-1;i++){
This clumps/merges every other ArrayList element
String newArr = "(" + MyList.get(i) + " "+ MyList.get(i + 1) + ")";
This updates the elements of the ArrayList
MyList.set(i, newArr);
This removes ArrayList elements at odd indices (e.g. 1,3...)
MyList.remove(i + 1);
}
This prints the updated List
System.out.println("Updated ArrayList: " + MyList);
}