java修复程序可以在不重复的情况下正确组织数据
我一直在处理文件中的数据,并将它们配对在一起。我需要按字母顺序对数据进行排序,同时将它们配对在一起。这是我的文本文件:
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;
}
}
# 1 楼答案
米希尔·凯卡尔是对的,这只是一个例子
定义联系人类
class ContactData implements Comparable<ContactData>
重写compareTo方法进行排序
public int compareTo(ContactData other)
制作联系人数组并使用
Arrays.sort(yourOwnContactArray);
这将使用重写的compareTo方法。虽然您有一个默认的排序方法,但仍然可以使用其他方法进行排序,比如
List<ContactData> listContacts = make-your-own ...
然后使用集合。使用其他/不同的比较器排序