Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在从文本文件逐行读取的链表中,按字母顺序对字符串排序的更好方法是什么?_Java_Sorting_Linked List - Fatal编程技术网

Java 在从文本文件逐行读取的链表中,按字母顺序对字符串排序的更好方法是什么?

Java 在从文本文件逐行读取的链表中,按字母顺序对字符串排序的更好方法是什么?,java,sorting,linked-list,Java,Sorting,Linked List,主要方法: public class ContactList { private ContactNode head; private ContactNode last; public ContactNode current; public ContactList value; public ContactList() {} public void addNode(ContactNode input) { if (this.he

主要方法:

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
节点类:

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. ");
        }
    }
}
我正在制作一个有趣的程序,从文本文件中读取联系人信息并将其放入链接列表中。我想创建一个排序方法来按字母顺序对每个节点或名称进行排序。我做了大量的研究,我的方法只打印如下代码:ContactList@282c0dbe,行数与我的文本文件的行数一样多。

,要漂亮地打印您的列表,您需要在ContactList类中实现toStringContactList@282c0dbe?

它是类名,后跟at符号,最后是哈希代码,是对象的哈希代码。Java中的所有类都直接或间接地从对象类继承。对象类有一些基本方法,如clone、toString、equals、,。。等等。对象中的默认toString方法打印“类名@哈希代码”

解决办法是什么

您需要重写ContactList类中的toString方法,因为它将以可读的格式为您提供有关对象的清晰信息,您可以理解这些信息

覆盖toString的优点是:

帮助程序员记录和调试Java程序

因为toString是在java.lang.Object中定义的,并且没有提供有价值的信息,所以它是 为子类重写它的良好实践

对于排序,您应该实现Comparator接口,因为您的对象没有。从更好的意义上讲,如果您想要定义外部可控的排序行为,这可以覆盖默认的排序行为

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;
    }
}
  @override
  public String toString(){
     // I assume name is the only field in class test
     return name + " " + index;
  }