Find out what the most frequently used class in the
Collections Framework is. I am 99.9% sure it will be
this. Create a text file with your words in and read it into a List<
String> or a String[] like this:-
The two code blocks differ in lines 10 and 15. Never mind the details of the above; look on them as reliable ways to create a
List<String> or a
String[] without having to write the words repeatedly. For the time being, forget about the numbers to count words. Your difficulty isn't reading words; it is in rearranging them with long words in the middle. You can sort the List and array by
word length like this (work out the
imports for yourself:-
Both those methods use a stable sort, so Anne and Joey won't be swapped. Again, don't go too far into the apparently confusing details.
Now, you have a reliable source for a List<String> or a String[] sorted by length. Get yourself paper and pencil and draw how you would insert the last element in the List in the middle of your destination List, or similar. Then work out where to put the penultimate element. Remember you are doing arithmeric with the length of the inputList/array.
If you go through the
documentation for Comparator, you will find
this, which may make things easier by sorting your List/array backwards. Remember that an
Object[] is filled with
nulls until you populate it with real values.