I am trying to finish my assignment, but I am not sure how to complete it. I have 3 arrays in my code, each of which have first names and last names (the last name comes before the first one in the arrays).
Which function should I use in order to decided whether a string is before the other? I was thinking compare, but I'm not too sure how to implement it.
This is my code so far:
public static void main(String[] args)
{
String[] section1 = {"Curie, Marie", "Feynman, Richard", "Germain, Sophie",
"Turing, Alan"};
String[] section2 = {"Bolt, Usain", "Graf, Steffi","Hamm, Mia"};
String[] section3 = {"Bach, Johann Sebastian", "Beethoven, Ludwig van",
"Mozart, Wolfgang Amadeus", "Schumann, Clara"};
String[] merged = mergeSortedArrays(section1, section2);
String[] merged = mergeSortedArrays(merged, section3);
for(int i = 0; i < merged.length(); i ++)
System.out.print(merged[i]);
}
//Do not change the method header
public static String[] mergeSortedArrays(String[] a1, String[] a2)
{
int i = 0, j = 0, k = 0;
while(a1[i] != null && a2[j] != null)
{
if(a1[i] "comes before" a2[j])
{
merged[k] = a1[i];
i++;
else {
merged[k] = a2[j];
j++;
}
k++;
}
return merged;
}
}