gpt4 book ai didi

java - 按字母顺序对数组进行排序

转载 作者:行者123 更新时间:2023-12-02 10:05:13 24 4
gpt4 key购买 nike

我正在尝试对数组进行排序(我必须使用数组),因为新数据进入数组(不是添加所有元素然后排序)。我的排序算法无法正常工作。它不返回排序数组,而是仅返回 3 个重复多次的条目。

这就是我要添加到数组中的内容:

    arrayDirectory.addEntry("Smith  RK  005598");
arrayDirectory.addEntry("Alal KA 004567");
arrayDirectory.addEntry("Bors OB 005678");
arrayDirectory.addEntry("Zaaa NZ 001234");
arrayDirectory.addEntry("Zoll NZ 001254");
arrayDirectory.addEntry("Ola KO 001245");

这是它返回的内容:

------PHONE DIRECTORY AS OF WED MAR 27 15:29:52 GMT 2019------
SURNAME: BORS INITIALS: OB NUMBER: 005678
SURNAME: BORS INITIALS: OB NUMBER: 005678
SURNAME: BORS INITIALS: OB NUMBER: 005678
SURNAME: BORS INITIALS: OB NUMBER: 005678
SURNAME: BORS INITIALS: OB NUMBER: 005678
SURNAME: SMITH INITIALS: RK NUMBER: 005598
SURNAME: SMITH INITIALS: RK NUMBER: 005598
SURNAME: SMITH INITIALS: RK NUMBER: 005598
SURNAME: SMITH INITIALS: RK NUMBER: 005598
SURNAME: SMITH INITIALS: RK NUMBER: 005598
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254
SURNAME: ZOLL INITIALS: NZ NUMBER: 001254

问题是什么?

这是我的 addEntry 方法:

@Override
public void addEntry(String line) {
String[] newLine = line.split("\\s+");
String surname, initial, number;
if (newLine.length == 3) {
surname = newLine[0];
initial = newLine[1];
number = newLine[2];

} else {
throw new IllegalArgumentException("Please fill all the required fields, [surname,initials,number]");
}

if (count == entries.length) {
Entry[] tempEntries = new Entry[2 * count];
System.arraycopy(entries, 0, tempEntries, 0, count);
entries = tempEntries;


} else {
Entry entry = new Entry(surname, initial, number);


for (int i = 0; i < entries.length; i++) {
for (int j = i + 1; j < entries.length; j++)
if (entries[j]!=null){
String one = entries[j].getSurname();
if (surname.compareToIgnoreCase(one) > 0) {
Entry temp = entries[i];
entries[i] = entries[j];
entries[j] = temp;
}
}
else {
entries[count]=entry;
count++;
}
}

}
}

这是我的打印方法:

public void printEntries()throws NullPointerException{
Date date=new Date();

System.out.println("------PHONE DIRECTORY AS OF "+date.toString().toUpperCase()+"------");

for(int i=0;i<entries.length;i++){
if(entries[i].getSurname()==null){
throw new NullPointerException("------END OF DIRECTORY------");
}else{
System.out.printf("SURNAME: %S INITIALS: %S NUMBER: %s %n",entries[i].getSurname(),entries[i].getInitial(),entries[i].getNumber());
}
}
}

任何帮助将不胜感激!

最佳答案

if (count ==entries.length) { 为 true 时,您只是增加数组的大小,但不插入任何元素。您应该在这两种情况下插入该元素。同样在 else 子句中,您多次添加该元素。 if (entries[j]!=null){ 将多次返回 false,最终您将一次又一次地添加该元素。

关于java - 按字母顺序对数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55381005/

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