gpt4 book ai didi

java - 排序名字和姓氏数组

转载 作者:行者123 更新时间:2023-11-30 11:08:37 25 4
gpt4 key购买 nike

我正在制作一个程序,我需要为飞机上的座位表制作方法对这个名字和姓氏列表进行排序。它是一个二维数组,第一列是名字,第二列是第一列对应人物的姓氏。我需要按姓氏、名字的字母顺序对其进行排序,但我不知道如何对姓氏进行排序,然后保留相应的姓氏。

这是我的代码:

package assignment_6_1;
import java.util.Scanner;
import java.util.Arrays;
public class Assignment_6_1 {

public static void main(String[] args) {
//Create a Scanner
Scanner input = new Scanner(System.in);
//Create int & string []
String[][]firstAndLastNames = new String[36][2];
int[]heightOfPerson = new int[36];
String[][]seatingChartDiagram = new String[9][4];
int[][]seatRequest = new int [36][2];

//Gather list of names and heights
for(int i=0; i<heightOfPerson.length; i++)
{

//Get first name
System.out.println("Enter Your First Name: ");
firstAndLastNames[i][0] = input.next();

//Get last name
System.out.println("Enter Your Last Name: ");
firstAndLastNames[i][1] = input.next();

//Get height in inches
System.out.println("Enter your height (Iches) : ");
heightOfPerson[i] = input.nextInt();

//Is there a seat request or not
System.out.println("Do you want to request a seat ?");
String ifSeatRequest = input.next();

//Get seat request
if(ifSeatRequest.equals("Yes") || ifSeatRequest.equals("yes"))
{
System.out.println("Enter the seat row you want: ");
seatRequest[i][0] = input.nextInt();
System.out.println("Enter the seat number you want in that row: ");
seatRequest[i][1] = input.nextInt();
}
else
{
//set the request colum for that person to 0 for no request
seatRequest[i][0] = 0;
seatRequest[i][1] = 0;
}
//Prints passenger manifest when list is full
if(firstAndLastNames[35][1] != null){
System.out.println("All Seats are Filled");
break;}
}
String[]firstNameSort = new String[36];
//put the first names into another array to sort
for(int j=0; j<heightOfPerson.length; j++)
{
firstNameSort[j] = firstAndLastNames[j][1];
}
//Alphabetize first name list
Arrays.sort(firstNameSort);
String[][]firstNameAlphLast = new String[36][2];
String[][]nameAlph = new String[36][2];
}
}

按顺序:它接收用户输入的 36 个姓名、高度以及用户是否想提出座位请求。我让它复制数组 2 另一个数组,然后我按字母顺序排列姓氏。现在我不知道如何为按字母顺序排列的姓氏获取相应的名字。

最佳答案

试试这个:-

Arrays.sort(firstAndLastNames, new Comparator<String[]>() {
@Override
public int compare(final String[] a1, final String[] a2) {
return a1[1].compareTo(a2[1]) + a1[0].compareTo(a2[0]);
}
});

关于java - 排序名字和姓氏数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28479689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com