Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Pointers_Linked List_Traversal - Fatal编程技术网

程序丢失Java链表指针

程序丢失Java链表指针,java,list,pointers,linked-list,traversal,Java,List,Pointers,Linked List,Traversal,我遇到了一个问题,我花了几个小时试图修复我的程序。我的链表可以工作,我可以向其中添加对象,这些对象在创建时都有以前的指针,但是当我试图按按钮遍历链表时,指针似乎不起作用。它将转到上一个节点,但随后将完全停止工作,没有任何异常。我真的无法理解什么是错误的,但我敢打赌这是非常简单的事情,比如一行代码出现在错误的地方或者其他什么 发动机等级: public TeamList tl = new TeamList(); public Team[] t = new Team[0]; private int

我遇到了一个问题,我花了几个小时试图修复我的程序。我的链表可以工作,我可以向其中添加对象,这些对象在创建时都有以前的指针,但是当我试图按按钮遍历链表时,指针似乎不起作用。它将转到上一个节点,但随后将完全停止工作,没有任何异常。我真的无法理解什么是错误的,但我敢打赌这是非常简单的事情,比如一行代码出现在错误的地方或者其他什么

发动机等级:

public TeamList tl = new TeamList();
public Team[] t = new Team[0];
private int x = 0;

public void createTeams() {
    x++;
    System.out.println("Enter team name:");
    String teamName;
    teamName = GUI.EditTeams.jTextField1.getText();
    t = Arrays.copyOf(t, (t.length + 1)); 
    t[x - 1] = new Team(x, teamName); /
    if (t[x - 1].getTeamNumber() == 1) { 
        tl.addTeam(t[x - 1]); 
        tl.current = t[x - 1];
    } else {
        tl.addTeam(t[x - 1]); 
        t[x - 1].setPrev(t[x - 2]);
        tl.current = t[x - 1];
    }
    printAllTeams(t);
    EditTeams.jTextField1.setText("");
    EditTeams.jTextField3.setText(t[x - 1].getTeamName());
    System.out.println("Team added!");
}
在上一次团队按钮按下时执行的GUI类操作:

    try {
        sm.prevTeam();
    } catch (InterruptedException ex) {
        Logger.getLogger(EditTeams.class.getName()).log(Level.SEVERE, null, ex);
    }


prevTeam():

public void prevTeam() throws InterruptedException {
    if (t.length == 0 || tl.current.getPrev() == null) {
        EditTeams.jTextField3.setText("Error: No more teams available");
        Thread.sleep(2000);
        EditTeams.jTextField3.setText("");
    } else {
        tl.traverse('p', t[x - 1]);
        EditTeams.jTextField3.setText(tl.current.getTeamName());
    }
}
以下是链接列表:

public class TeamList {

public Team head;
public Team current;

public void addTeam(Team newTeam) // Method to add an item
{
    if (head == null) // If the head is null then head becomes the new item
    {
        head = newTeam;
    } else {  // Else the current is the head
        current = head;
        while (current.getNext() != null) // While the next node is not null, set the current node as the next node
        {
            current = current.getNext();
            current.setNext(newTeam); // Once at the end, the current node becomes the new item
        }
    }
}

public void traverse(char nextOrPrev, Team team)
{
    if (nextOrPrev == 'n') {
        current = team.getNext();
    } else if (nextOrPrev == 'p') {
        current = team.getPrev();
    }
    //team.position = current;
    //current. = p;
}
}


对不起,如果我做错了什么,我不是StackOverflow专业人士,也不是编程专业人士。请不要嘲笑我编写的代码。我看到有人说,这些东西应该标记为家庭作业帮助。这是家庭作业帮助。非常感谢。

我看到一些问题。首先,在将团队添加到引擎类中的团队列表t1之前,您从未设置过前一个团队

其次,在addTeamnewTeam方法中,您完全重置了每个团队的下一个团队。你需要移动线路

current.setNext(newTeam);
在for循环之外执行,以便它只在到达末尾时按您所说的方式执行


第三,当你遍历你的团队时,你根本没有遍历任何东西。相反,您给它一个向前和向后的字符键,然后将当前设置为输入组并继续。遍历方法通常有一个循环,从头部开始遍历整个列表,并对每个团队执行某些操作。照目前的情况,我不确定您的导线测量方法应该实现什么功能。

我将回答我自己的问题,以供任何关注此问题的人使用。问题在于调用prevTeam方法中的traverse方法

以下一行:

tl.traverse('p', t[x - 1]);
应该是:

tl.traverse('p',tl.current);

基本上,我每次都要从最近添加的团队中遍历,因此如果有两个团队,则不断重复到第1团队,因为第1团队是t[x-1]的前一个团队。我使用的是最新的数组对象,而不是链表

在AdGeTeb中,你从来没有设置过以前的链接。除非这是一个链接列表中的练习,考虑使用标准的java。UTI.LIKEDLIST。@ Henry He说这是家庭作业帮助。我想我们可以安全地假设这是链表中的一个练习。如果完成了,我应该将上一个链接放在哪里,以及如何将该链接放入列表方法中?我应该在创建新节点时传递它吗?非常感谢亨利。下次我会记得用的。