Starting to think I'm using the wrong book for learning. I've copied this word-for-word from Sam's Learn Java, but the .sort(names); method is undefined for type of array.
I have a feeling it's to do with the public static void main(String[] args) { call, but I don't know how to amend it.
package arrays;
import java.util.*;
public class Arrays {
public static void main(String[] args) {
String names[] = { "Lauren", "Audrina", "Heidi", "Whitney",
"Stephanie", "Spencer", "Lisa", "Brody", "Frankie", "Holly",
"Jordan", "Brian", "Jason" };
System.out.println("The original order:");
for (int i = 0; i < names.length; i++) {
System.out.print(i + ": " + names[i] + " ");
}
Arrays.sort(names);
System.out.println("\nThe new order:");
for (int i = 0; i < names.length; i++) {
System.out.print(i + ": " + names[i] + " ");
}
System.out.println();
}
}
Arraysthen, or at least qualify the secondjava.util.Arrays.Arrays.toString(names)instead of indexed iteration.toStringmeans you get whatever thetoStringimplementation gives you, uncustomized, without extra data that might be needed, with the brackets, etc.String.joinif you're on JDK 8, possibly, or a util lib, but even that doesn't give you the ability to access the index.