Java 列表节点找不到添加项的索引

标签 java

我刚刚学习 Java,并用 Java 创建了自己的单词列表和单词节点类。但是,我发现我的 WordList Index Of 方法始终返回 -1。有人可以告诉我有什么问题吗?与使用扫描仪有关吗? (我的教科书说这不是我的添加方法)。我的代码如下(每​​个类都在其自己的文件中):

WordNode.java:

public class WordNode {

private String value;
private WordNode next;

public WordNode(String newValue) {
    value = newValue;
    next = null;
}

public WordNode(String newValue, WordNode nextNode) {
    value = newValue;
    next = nextNode;
}

public String getValue() {
    return value;
}

public WordNode getNextNode() {
    return next;
}

public void setNextNode(WordNode node) {
    next = node;
}}

WordList.java:

public class WordList {
private WordNode first;

public WordList(String firstNode) {
    first = new WordNode(firstNode);
}

public void add(String newValue) {
    first = new WordNode(newValue, first);
}

public void remove(String oldValue) {
    if (first.getValue() == oldValue) {
        first = first.getNextNode();
        return;
    }
    WordNode temp = first;
    while (temp != null && temp.getNextNode().getValue() != oldValue)
        temp = temp.getNextNode();
    if (temp != null)
        temp.setNextNode(temp.getNextNode().getNextNode());
}

public void moveToStart(String toMove) {
    remove(toMove);
    add(toMove);
}

public int indexOf(String item) {
    WordNode temp = first;
    int i = 1;
    while (temp != null && temp.getValue() != item) {
        temp = temp.getNextNode();
        i++;
    }
    if (temp == null)
        return -1;
    else
        return i;
}

public String itemAtIndex(int index) {
    WordNode temp = first;
    int i = 1;
    while (temp != null && i != index) {
        temp = temp.getNextNode();
        i++;
    }
    if (i == index)
        return temp.getValue();
    else
        return "";
}}

MTFencoder.java:

import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class MTFencoder {

public static void main(String[] args) {
    try
    {
        Scanner scan = new Scanner(new FileReader(args[0]));
        WordList dictionary = new WordList(scan.next());

        while (scan.hasNext()) {
            String newWord = scan.next();
            int index = dictionary.indexOf(newWord);
            if (index == -1) {
                System.out.println("0 " + newWord);
                dictionary.add(newWord);
            }
            else {
                System.out.println(Integer.toString(index));
                dictionary.moveToStart(newWord);
            }
        }
    }
    catch (Exception ex) {
        System.out.println("An error occured while reading the file. Check that it exists and is valid.");
    }
}}

提前致谢。 丹尼尔

最佳答案

在Java中,要检查对象(包括字符串)的相等性,必须使用.equals方法。因此,在indexOf和remove中,您必须使用first.getValue().equals(oldValue)或类似的。

关于Java 列表节点找不到添加项的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10117964/

相关文章:

java - 用一些已知的概率,设置一个变量的值

java - 抽象方法中的参数 (Java 7)

java - 当 Activity 背景设置为系统壁纸时,如何更新 Activity 背景?

java - 在虚拟机中下载 Java 是否安全?

java - 有谁知道 javaspec.org 发生了什么?

java - 如何使用 JAXB 创建和解析 XML?

java - Apache POI 无法保存 (HWPFDocument.write) 大型 Word 文档文件

java - 动画后 Android 按钮不响应

java - 不同帧率游戏版本中LibGDX的差异

java - 使用 Web 浏览器延迟 JOptionPane