问题
To avoid the duplicate claims, I've looked at this post and it isn't exactly what I wanted.
Every other 2D ArrayList question involved double
or int
numbers; my question is about Strings
.
What I'm doing
I have a 2D ArrayList, defined like this:
ArrayList<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();
The idea is that the first item in every row of ArrayLists contains the names, and the rest of the columns in each row contains the phone numbers (unknown amount). Therefore I would like to avoid converting it to a regular array.
Sample
Let's say I've populated my ArrayList and I have this:
{"Mike", "(805) 766-4920"}
{"Emily", "(705) 668-9292", "(705) 555-1060"}
{"James", "(605) 965-2000"}
I expect my output to be this:
{"Emily", "(705) 668-9292", "(705) 555-1060"}
{"James", "(605) 965-2000"}
{"Mike", "(805) 766-4920"}
I want to keep the numbers corresponding with the names, but simply sort the array by the name.
What answers I hope for
I'm hoping for a built-in function manipulation type of thing, but I'd be fine if someone created a sorting array method for me with an 2D ArrayList as the input. I haven't seen any question that answers this issue explicitly. I will continue trying to come up with an answer myself as well.
回答1:
You can use Collections.sort and provide a custom Comparator where you compare the first element of each list, i.e the name :
List<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Mike", "(805) 766-4920")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("James", "(605) 965-2000")));
Collections.sort(namesAndNumbers, new Comparator<ArrayList<String>>() {
@Override
public int compare(ArrayList<String> o1, ArrayList<String> o2) {
return o1.get(0).compareTo(o2.get(0));
}
});
System.out.println(namesAndNumbers);
Output :
[[Emily, (705) 668-9292, (705) 555-1060], [James, (605) 965-2000], [Mike, (805) 766-4920]]
回答2:
Create a custom comparator:
final Comparator<List<String>> comparator = new Comparator<List<String>>() {
public int compare(List<String> pList1, List<String> pList2) {
return pList1.get(0).compareTo(pList2.get(0));
}
};
final List<List<String>> lists = Arrays.asList(
Arrays.asList("Mike", "(805) 766-4920"),
Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060"),
Arrays.asList("James", "(605) 965-2000")
);
Collections.sort(lists, comparator);
for (List<String> list : lists) System.out.println(list);
回答3:
For diversity Why dont you just use a HashMap unless you want duplicate keys. the key would be name and the value would be an ArrayList of numbers
String name="John";
ArrayList<String> phonenumbers=new ArrayList<String>();
phonenumbers.add("80925887");
phonenumbers.add("+166476654");
TreeMap<String, ArrayList> tm=new TreeMap<String, ArrayList>();
tm.put(name, phonenumbers);
来源:https://stackoverflow.com/questions/20480723/how-to-sort-2d-arrayliststring-by-only-the-first-element