Java 链表包含文本文件中的行。创建find(int-index)方法,该方法在作为参数传递的索引位置打印内容

Java 链表包含文本文件中的行。创建find(int-index)方法,该方法在作为参数传递的索引位置打印内容,java,linked-list,Java,Linked List,请查找以下代码段,用于打印作为参数传递的索引位置的内容。准备的列表包含从文本文件读取的行。看起来像 [美国广播公司。。。。。。 .... ] 如果有什么问题,请改正 public class ContactList { private ContactNode head; private ContactNode last; public ContactNode current; public ContactList() {} public void addN

请查找以下代码段,用于打印作为参数传递的索引位置的内容。准备的列表包含从文本文件读取的行。看起来像 [美国广播公司。。。。。。 .... ]

如果有什么问题,请改正

public class ContactList {
    private ContactNode head;
    private ContactNode last;
    public ContactNode current;
    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 getFirst() {
        current = this.head; 
        while (current != null) {  
            System.out.print("The first contact of the list is: "
                        + current.getName());
            break;
        }
    } 
    public void getLast(){      
        current = this.last; 
        while (current != null) {  
            System.out.print("The Last contact of the list is: " 
                        + current.getName());
            break;
        }
    }
    public ContactNode find(int index) {
        ContactNode current = head;
        while(current!=null) {
            if(current.getIndex() == (index))
                System.out.println(current.getName());
            else 
                current = current.getNext();
            System.out.println(current);
        }
            return null;
    }
}
提前谢谢

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;}

ContactNode(){}

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;}

}
主要方法类:

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.getFirst();
myList.getLast();
myList.find(3);

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

不要在find方法中打印,而是实际返回找到的节点。
另外,还不清楚您的
ContactNode
类是否确实有
getIndex()
方法,因此我假设它没有。为了弥补这一点,我们记录了一次计数(head=0)


我还在主方法类中使用scanner类。这个find方法不返回任何内容。我试图让用户输入索引,该方法从列表中获取索引,并打印列表中该点的值或联系人。该程序还包含两个其他程序:主方法类和节点类。我在main方法中创建了这个列表,并在名为ContactNode.java的节点类和名为ContactMain.javaHey的mainmethodclass中创建了getIndex()和setIndex()方法。谢谢这些指针。我喜欢你的建议。它比我的find方法好,因为它不那么凌乱。但是,我的程序在ContactNode类中确实包含getIndex()和setIndex()方法。同样的问题也会发生,因为它根本不打印任何内容。也许我也应该上传所有的程序?是的,继续吧,把它们都放在你原来的帖子里。好的@Mshnik我已经成功上传了额外的程序来帮助你进一步评估。谢谢你的帮助,我已经解决了我的问题。它可以与打印语句配合使用(这是坏习惯吗?编码不好?)。再次感谢你。这并不是很糟糕,但是一定要问问你自己,它是在工作,还是仅仅从打印输出来看是在工作。正如在中一样,尝试打印关于列表的其他内容,这些内容在一些操作之后应该是真实的。没问题!感谢您的支持/接受。
public ContactNode find(int index) {
        ContactNode current = head;
        while(current!=null) {
            if(index == 0)
                return current;

            //we're not there yet. Move to next node, decrement index
            current = current.getNext();
            index--;
        }
        return null;
}