I have two arrays of strings that I am trying to combine into one array. The two arrays are in alphabetical order. I have to combine them into alphabetical order. The way I attempted to do this was to create the combined list as the first list followed by the second list, and then sort them. Unfortunately, my instructor for my class says they want me to combine and sort them in one single step using the compareTo method. Below is my code, how would I accomplish this? The first two arrays are user inputted values in alphabetical order of up to 10,000 words with the rest as null, so for example:
list1 = {"Alfred", "Bev", "Carl", "Dan", null, etc.)
list2 = {"Bob", "Craig", "Dean", "Fran", null, etc.)
list3 goal: {"Alfred", "Bev", "Bob", "Carl", Craig", "Dan", "Dean", "Fran"}
for (int b = 0; b < list3.length; b++)//adds list1 to merged array
{
if (list1[b] != null) {
list3[b] = list1[b];
f++;
}
}
int x = 0;
for (int y = f; y < list3.length; y++)//adds list2 to merged array
{
if (list2[x] != null) {
list3[y] = list2[x];
x++;
}
}
for (int q = 0; q < list3.length; q++)//Merged array in alphabetical order
{
if (list3[q] != null) {
for (int b = q; b < list3.length; b++) {
if (list3[b] != null) {
if (list3[q].compareTo(list3[b]) > 0) {
String s = list3[q];
list3[q] = list3[b];
list3[b] = s;
}
}
}
}
}
This is my first time using Stack Exchange so hopefully everything is formatted correctly! Sorry for any errors.