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

时间:2019-03-27 15:33:02

标签: java arrays sorting

我正在尝试对数组进行排序(我必须使用数组),因为新数据进入了数组(不添加所有元素然后进行排序)。我的排序算法无法正常工作。 而不是返回已排序的数组,它仅返回重复多次的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());
    }
    }
    }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

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