208k views
5 votes
Language/Type: Java ArrayList Collections

Author: Marty Stepp (on 2016/09/08)
Write a method swapPairs that switches the order of values in an ArrayList of Strings in a pairwise fashion. Your method should switch the order of the first two values, then switch the order of the next two, switch the order of the next two, and so on. For example, if the list initially stores these values: {"four", "score", "and", "seven", "years", "ago"} your method should switch the first pair, "four", "score", the second pair, "and", "seven", and the third pair, "years", "ago", to yield this list: {"score", "four", "seven", "and", "ago", "years"}
If there are an odd number of values in the list, the final element is not moved. For example, if the original list had been: {"to", "be", "or", "not", "to", "be", "hamlet"} It would again switch pairs of values, but the final value, "hamlet" would not be moved, yielding this list: {"be", "to", "not", "or", "be", "to", "hamlet"}
Language/Type: Java ArrayList Collections
Author: Marty Stepp (on 2016/09/08)
Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list.
Type your solution here:
1. for (Iterator iterator = numbers.iterator(); iterator.hasNext();)
2. { Integer number = iterator.next();
3. if (number % 2 == 0) {
4. System.out.println("This is Even Number: " + number);
5. iterator.remove(); mtino
6
7
8 } This is a method problem.
Write a Java method as described. Do not write a complete program or class; just the method(s) above.
Language/Type: Java ArrayList Collections
Author: Marty Stepp (on 2016/09/08)
Write a method removeDuplicates that takes as a parameter a sorted ArrayList of Strings and that eliminates any duplicates from the list. For example, suppose that a variable called list contains the following values: {"be", "be", "is", "not", "or", "question", "that", "the", "to", "to"} After calling removeDuplicates (list); the list should store the following values: {"be", "is", "not", "or", "question", "that", "the", "to"}
Because the values will be sorted, all of the duplicates will be grouped together.
Type your solution here:
WN 600 O
This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

User Alexxjk
by
5.4k points

1 Answer

3 votes

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.

User Akua
by
5.5k points