Java getNext() 链表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20154544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 23:33:20  来源:igfitidea点击:

getNext() Linked List

javalistlinked-list

提问by user3023346

I'm really new to Java and StackOverflow so please don't be mean. I would really appreciate some help. Thank you in advanced.

我对 Java 和 StackOverflow 真的很陌生,所以请不要吝啬。我真的很感激一些帮助。先谢谢了。

I feel like this is really easy and I've already tried this out a million different ways but it's not working.

我觉得这真的很容易,而且我已经尝试过一百万种不同的方法,但它不起作用。

I am trying to take in a text file and store it into a linked list and I'm trying to access the third node of this linked list. For some reason, I can access the first node, then I can use the getNext() command to go to the next node, but when I try to use getNext() to go the third node, it continues to return the second node. So it is not going to third node. Am I just missing some key concept? Also let me know if you need anymore information.

我正在尝试接收一个文本文件并将其存储到一个链表中,并且我正在尝试访问该链表的第三个节点。由于某种原因,我可以访问第一个节点,然后我可以使用 getNext() 命令转到下一个节点,但是当我尝试使用 getNext() 转到第三个节点时,它继续返回第二个节点。所以它不会去第三个节点。我只是错过了一些关键概念吗?如果您需要更多信息,也请告诉我。

The text file that is being taken in is: 5 A B C D E A B //This is the line that I want B C B D C D C E D E

被取入的文本文件是:5 ABCDEAB //This is the line that I want BC BD CD CE DE

Here is part of my code:

这是我的代码的一部分:

public static void main(String[] args) throws IOException{
    /**
     * Check whether the user types the command correctly
     */
    if (args.length != 1)
    {

        System.out.println("Invalid input");
        System.out.println(args.length);
        System.exit(1);
    }

    String filename = args[0];
    Scanner input = new Scanner (new File(filename));

            LinkedList<String> linkedList= new LinkedList<String>();

            while(input.hasNext())
    {
        linkedList.addToRear(input.nextLine());
    }

            LinearNode<String> link= linkedList.firstLink;

            String temp = " ";
    link.getNext();
    temp = (String)link.getElement();
    String[] numofVerticesArray = temp.split(" ");
    int numOfVertices = Integer.parseInt(numofVerticesArray[0]);
    int lineNumber = 1;

    String [] arrayOfVertices; 
    LinearNode<String> secondLine = link;
    String temp2;


    for (int i=0; i <= lineNumber; i++)
    {
        secondLine = link.getNext();
    }
    lineNumber = 2;
    temp2 = (String)secondLine.getElement();
    arrayOfVertices = temp2.split(" ");

            int[][] adjMatrix = new int[numOfVertices][numOfVertices];

    LinearNode<String> edgeLine = link;
    String [] arrayOfEdge;
    int rowCount = 0;
    int columnCount = 0;
    String temp3;
    lineNumber = 2;

    for (int i=0; i <= lineNumber; i++)
    {
        edgeLine = link.getNext();
        System.out.print((String)edgeLine.getElement());
                    //When this is printed out, the second node's 
                    //content is printed out, not the third node
    }
    lineNumber++;
    temp3 = (String)edgeLine.getElement();
    arrayOfEdge = temp3.split(" ");

采纳答案by Quillion

You keep on asking for the second element from the LinkedList.

您继续从 LinkedList 中请求第二个元素。

edgeLine = link.getNext();

sets the value of second element of the LinkedList link into edgeLine, and then you loop and do the same, and then the same over and over and over again.

将 LinkedList 链接的第二个元素的值设置为edgeLine,然后循环并执行相同的操作,然后一遍又一遍地重复相同的操作。

Try doing

尝试做

edgeLine = edgeLine.getNext();

This will keep on moving.

这将继续前进。

回答by Andrei Nicusan

The only assignment you make to the linkvariable is this: link= linkedList.firstLink;. You're never assigning anything else to it. So calling link.getNext()will always return the same node, namely the second one. linkis not an iterator, thus you can't call getNext()and advance through the linked list.

您对link变量所做的唯一分配是:link= linkedList.firstLink;。你永远不会为它分配任何其他东西。所以调用link.getNext()将始终返回相同的节点,即第二个。link不是迭代器,因此您无法调用getNext()和推进链表。