The program prompts the user for two word list, each word is separated by a white space. The program when mixes these two list (one word from list one and then one word from list two and next word from list one, and so on) and form a longer list. If one list is shorter than the other, add all remaining words from the longer list after all the words from the shorter list have been mixed in. The program then output the long list.
Below are some sample runs.
Example 1 (first list is longer than second list):
Please enter the first word list:
one two three four
Please enter the second word list:
five six seven
one five two six three seven four
Example 2 (first list is shorter than second list):
Please enter the first word list:
dog cat fish
Please enter the second word list:
bear cow hog chicken goat
dog bear cat cow fish hog chicken goat
Example 3 (lists are the same length)
Please enter the first word list:
apple banana pear peach
Please enter the second word list:
orange grape kiwi cherry
apple orange banana grape pear kiwi peach cherry
Requirements The result (mixed word list) must be stored in an ArrayList.
Below is what I have so far.
import java.util.Scanner;
import java.util.ArrayList;
public class ArrayMixer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first word list: ");
String input1 = sc.nextLine();
System.out.println("Please enter the second word list: ");
String input2 = sc.nextLine();
String[] array1 = input1.split(" ");
String[] array2 = input1.split(" ");
ArrayList<String> mixedArray = new ArrayList<>();
for (int i=0; i<array1.length; i ) {
mixedArray.add(array1[i]);
mixedArray.add(array2[i]);
}
}
}
CodePudding user response:
Take a look at my solution ( you will find the explication of the solution as comment in the code ) :
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first word list: ");
String input1 = sc.nextLine();
System.out.println("Please enter the second word list: ");
String input2 = sc.nextLine();
String[] array1 = input1.split(" ");
String[] array2 = input2.split(" ");//you have to use input2 instead of input1
//convert array to list
List<String> list1 = new ArrayList<>(Arrays.asList(array1));
List<String> list2 = new ArrayList<>(Arrays.asList(array2));
ArrayList<String> mixedArray = new ArrayList<>();
//the of the small array to avoid indexOutofboundException
int length = array1.length - array2.length <= 0 ? array1.length : array2.length;
for (int i = 0; i < length; i ) {
mixedArray.add(array1[i]);
mixedArray.add(array2[i]);
//every element added to the array will be removed from the list
list1.remove(0);
list2.remove(0);
}
// add the the rest of element from the list that contains more than the others
mixedArray.addAll(list1);
mixedArray.addAll(list2);
System.out.println(mixedArray.toString());
}
Output:
**Example 1:**
Please enter the first word list:
one two three four
Please enter the second word list:
five six seven
[one, five, two, six, three, seven, four]
**Example 2:**
Please enter the first word list:
dog cat fish
Please enter the second word list:
bear cow hog chicken goat
[dog, bear, cat, cow, fish, hog, chicken, goat]
**Example 3:**
Please enter the first word list:
apple banana pear peach
Please enter the second word list:
orange grape kiwi cherry
[apple, orange, banana, grape, pear, kiwi, peach, cherry]
**and so on ...**
CodePudding user response:
import java.util.Scanner;
import java.util.ArrayList;
public class ArrayMixer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first word list: ");
String input1 = sc.nextLine();
System.out.println("Please enter the second word list: ");
String input2 = sc.nextLine();
String[] array1 = input1.split(" ");
String[] array2 = input1.split(" ");
ArrayList<String> mixedArray = new ArrayList<>();
for (int i=0; i < Math.max(array1.length, array1.length); i ) {
mixedArray.add(array1[i]);
mixedArray.add(array2[i]);
}
}
}