gpt4 book ai didi

java - 修复了保持数据正确组织而不重复的问题

转载 作者:行者123 更新时间:2023-12-01 17:28:53 25 4
gpt4 key购买 nike

我一直在处理文件中的数据并将它们配对在一起。我需要按字母顺序对数据进行排序,同时使它们同时配对。这是我的文本文件:

Joe
Jake
Toronto
32
joejake@gmail.com
Bob
Barley
Vancouver
13
bobbarley@gmail.com
Felix
Fixed
Boston
24
felixfixed@gmail.com

这就是我需要的输出:

First Name:
Bob
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
bobbarley@gmail.com
First Name:
Felix
Last Name:
Fixed
City:
Boston
Age:
24
Email:
felixfixed@gmail.com
First Name:
Joe
Last Name:
Jake
City:
Toronto
Age:
32
Email:
joejake@gmail.com

理想情况下,当我对它们进行排序时,名字和姓氏等将正确配对在一起。但是,我目前的输出结果是:

First Name:
Bob
Last Name:
Barley
City:
Vancouver
Age:
13
Email:
bobbarley@gmail.com
First Name:
Felix
Last Name:
Fixed
City:
Boston
Age:
24
Email:
felixfixed@gmail.com
First Name:
Joe
Last Name:
Fixed
City:
Boston
Age:
24
Email:
felixfixed@gmail.com

这是我编写的代码:

public static void insertfname (String fname [], String lname [], String city [], String age [], String email []) throws IOException
{
int howMany = 0;
int count = count (howMany);

for (int i = 1; i < count; i++)
{
String current = fname [i];
String current2 = lname [i];
String current3 = city [i];
String current4 = age [i];
String current5 = email [i];
int j = i - 1;
//Insertion sort and keeping records together
while (j >= 0 && current.compareToIgnoreCase (fname [j]) < 0)
{
fname [j + 1] = fname [j];
j--;
}
fname [j + 1] = current;

while (j >= 0 && current.compareToIgnoreCase (fname [j]) < 0)
{
lname [j + 1] = lname [j];
j--;
}
lname [j + 1] = current2;

while (j >= 0 && current.compareToIgnoreCase (fname [j]) < 0)
{
city [j + 1] = city [j];
j--;
}
city [j + 1] = current3;

while (j >= 0 && current.compareToIgnoreCase (fname [j]) < 0)
{
age [j + 1] = age [j];
j--;
}
age [j + 1] = current4;

while (j >= 0 && current.compareToIgnoreCase (fname [j]) < 0)
{
email [j + 1] = email [j];
j--;
}
email [j + 1] = current5;
}
}

最佳答案

Mihir Kekkar 是对的,这只是一个例子;

定义接触类,

class ContactData implements Comparable<ContactData>

重写compareTo方法进行排序

public int compareTo(ContactData other)

创建联系人数组并使用

对其进行排序

Arrays.sort(yourOwnContactArray);

这使用了重写的compareTo方法。虽然您有默认的排序方法,但您仍然可以使用其他方法进行排序,例如;

List<ContactData> listContacts = make-your-own ...

然后将 Collections.sort 与其他/不同的比较器一起使用

`Collections.sort(listContacts, (a,b) -> {
return a.lastName.concat(a.name).compareTo(b.lastName.concat(b.name));
});`

关于java - 修复了保持数据正确组织而不重复的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61163706/

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