简体   繁体   中英

What is a better method to sort strings alphabetically in a linked list that is reading in lines from a text file?

public class ContactList {

    private ContactNode head;
    private ContactNode last;
    public ContactNode current;
    public ContactList value;

    public ContactList() {}

    public void addNode(ContactNode input) {
        if (this.head == null) {
            this.head = input;
            this.last = input;
        } else last.setNext(input);
        input.setPrev(last);
        this.last = input;
    }

    public void traverse() {
        System.out.println();
        current = this.head;
        while (current != null) {
            System.out.print(current.getName() + " ");
            System.out.println("");
            current = current.getNext();
        }
        System.out.println();
    }


    public void insertNewFirstNode(String value) {
        ContactNode newNode = new ContactNode(value);
        head = newNode;
        if (last == null) {
            last = head;
        }
    }

    public void sort() {
        ContactList sorted = new ContactList();
        current = head;
        while (current != null) {
            int index = 0;

            if ((current.getName() != null)) {
                index = this.current.getName().compareTo(current.getName());
                if (index == 1) {
                    sorted.insertNewFirstNode(current.getName());
                }
                current = current.getNext();
            } else if ((current != null)) {
                System.out.print(sorted + "\n");
            }
        }
    } // end contactList

Main Method:

import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;

public class ContactMain {
    public static void main(String[] args) {
        try {
            FileReader filepath = new FileReader("data1.txt");
            Scanner k = new Scanner(filepath);
            ContactList myList = new ContactList();
            while (k.hasNextLine()) {
                String i = k.nextLine();
                myList.addNode(new ContactNode(i));
            }

            myList.traverse();
            myList.sort();
            myList.traverse();


        } catch (FileNotFoundException e) {
            System.out.println("File Not Found. ");
        }
    }
}

Node Class:

public class ContactNode {

    private String name;
    public int index;
    private ContactNode prev;
    public ContactNode next;

    ContactNode(String a) {
        name = a;
        index = 0;
        next = null;
        prev = null;
    }

    public ContactNode getNext() {
        return next;
    }

    public ContactNode getPrev() {
        return prev;
    }

    public String getName() {
        return name;
    }

    public int getIndex() {
        return index;
    }

    public void setNext(ContactNode newnext) {
        next = newnext;
    }

    public void setPrev(ContactNode newprevious) {
        prev = newprevious;
    }

    public void setName(String a) {
        name = a;
    }

    public void setIndex(int b) {
        index = b;
    }
}

I am making a program for fun that reads in contact information from a text file and puts them into a Linked List. I want to create a sort() method to sort each node or name alphabetically. I've done a good amount of research and my method only prints code like: ContactList@282c0dbe , by as many lines as my text file.

您需要自定义Comparator进行排序 ,并且要漂亮地打印List您需要在ContactList类中实现toString()

what is ContactList@282c0dbe ?

It is class name follow by at sign and hash code at the end, hash code of the object.All classes in Java inherit from the Object class, directly or indirectly . The Object class has some basic methods like clone(), toString(), equals(),.. etc. The default toString() method in Object prints “class name @ hash code”.

What is the solution ?

You need to override toString method in ContactList class because it is going to give you clear information about the object in readable format that you can understand.

The merit about overriding toString :

Help the programmer for logging and debugging of Java program

Since toString is defined in java.lang.Object and does not give valuable information, so it is good practice to override it for subclasses.

  @override
  public String toString(){
     // I assume name is the only field in class test
     return name + " " + index;
  }

For sorting, you should implement Comparator interface since your object does not have natural ordering . In better sense, if you want to define an external controllable ordering behavior, this can override the default ordering behavior

read more about Comparator interface

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM