我有这段代码可以为不同的卡片组(俱乐部、钻石、红心和黑桃)对我的 arrayLists 进行排序。有没有办法编写以下代码,所以我不必为每个 ArrayList 编写它?
String clubsLabel = "Clubs";
String diamondsLabel = "Diamonds";
String heartsLabel = "Hearts";
String spadesLabel = "Spades";
Collections.sort(clubs, new cardIdSorter());
System.out.printf("%-12s", clubsLabel);
for(PlayingCard clubsCard: clubs) {
System.out.print(clubsCard);
}
System.out.println(" ");
Collections.sort(diamonds, new cardIdSorter());
System.out.printf("%-12s", diamondsLabel);
for(PlayingCard diamondsCard: diamonds) {
System.out.print(diamondsCard);
}
System.out.println(" ");
Collections.sort(hearts, new cardIdSorter());
System.out.printf("%-12s", heartsLabel);
for(PlayingCard heartsCard: hearts) {
System.out.print(heartsCard);
}
System.out.println(" ");
Collections.sort(spades, new cardIdSorter());
System.out.printf("%-12s", spadesLabel);
for(PlayingCard spadesCard: spades) {
System.out.print(spadesCard);
}