如下:看看满不满足要求,不对追问。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n > 50) {
System.out.print("No more than 50 contacts");
in.close();
return ;
}
ArrayList> list;
HashMapmap = new HashMap<>();
//String [][]contact = new String [n][2];
for(int i = 0; i < n; i ++) {
String [] temp = new String [2];
temp[0] = in.next();
if(temp[0].length() > 10) {
temp[0] = temp[0].substring(0, 10);
}
temp[1] = in.next();
if(temp[1].length() > 10) {
temp[1] = temp[1].substring(0, 10);
}
map.put(temp[0], temp[1]);
}
in.close();
list = new ArrayList<>(map.entrySet());
Collections.sort(list, new Compare());
for(int i = 0; i < n; i ++) {
System.out.println(format(list.get(i).getKey()) + format(list.get(i).getValue()));
}
}
static String format(String s) {
char [] c = new char[12 - s.length()];
for(int i = 0; i < c.length; i ++) {
c[i] = '#';
}
return String.valueOf(c) + s;
}
static class Compare implements Comparator> {
@Override
public int compare(HashMap.Entryo1, HashMap.Entry o2) {
int temp = o1.getKey().compareTo(o2.getKey());
if (temp > 0) {
return 1;
} else if (temp == 0) {
return 0;
} else {
return -1;
}
}
}
}
结果: