【问题标题】:IndexOutOfBoundsException for LinkedListLinkedList 的 IndexOutOfBoundsException
【发布时间】:2014-04-09 01:03:29
【问题描述】:

我正在尝试根据JList 中选择的值将文本字段的文本设置为字符串。

  • JList = list
  • LinkedList<WordPair> = wordpair_list
  • WordPair 包含 wordAwordB

如果有人能向我解释为什么这不起作用,我将永远欠你的债。该程序中显然有更多代码,但stackoverflow 似乎认为我的文本与代码的比例不成比例。如果您个人想要其余代码,如果您愿意接受挑战,我很乐意将其发送给您。

public void showTranslation(){
    int i = wordpair_list.indexOf(list.getSelectedValue());
    textField.setText(wordpair_list.get(i).getWordB());
}

public Dictionary(Object o){ 
    if (o instanceof String){ 
        String filename = (String) o; 
        File file = new File(filename); 
        Scanner sc = null; 

        try { 
            sc = new Scanner(file); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
        } 

        while (sc.hasNextLine()){ 
           words.add(new WordPair(sc.nextLine())); 
        } 
    } 
}

public WordPair(String arg0) { 
    arg0.trim(); 
    int equalsIndex = arg0.indexOf("="); 
    this.wordA = arg0.substring(0, equalsIndex-1); 
    this.wordB = arg0.substring(equalsIndex+1); 
}

【问题讨论】:

  • 发布您的异常、完整的调用跟踪以及它指向的部分代码。
  • 似乎Map<String, String>List<WordPair> 更适合您的目的。
  • 'code' public Dictionary(Object o){ if (o instanceof String){ String filename = (String) o;文件文件 = 新文件(文件名);扫描仪 sc = null;尝试 { sc = new Scanner(file); } catch (FileNotFoundException e) { e.printStackTrace(); } while (sc.hasNextLine()){ words.add(new WordPair(sc.nextLine())); } } } 和... public WordPair(String arg0){ arg0.trim(); int equalsIndex = arg0.indexOf("="); this.wordA = arg0.substring(0, equalsIndex-1); this.wordB = arg0.substring(equalsIndex+1); }
  • 我似乎无法让它以代码格式发布代码,抱歉

标签: java swing indexoutofboundsexception


【解决方案1】:

您的wordpair_list 不包含list.getSelectedValue() 返回的任何内容。请注意,当指定的对象不包含在列表中时,indexOf() 方法将返回 -1

您可以通过打印/记录i 的值来确认这一点。

在修复代码方面,如果wordpair_list 实际上确实包含list.getSelectedValue(),您的代码可以更简洁地写成:

public void showTranslation(){
    textField.setText(list.getSelectedValue().getWordB());
}

但由于该值不在列表中,您必须尝试其他方法。一种可能是您混淆了您的类型,并在wordpair_list 中查找不兼容类型的对象。另一种可能性是您需要覆盖equals()(因此也需要覆盖hashCode()),以便您的indexOf() 查找成功。

关于最后一点的更多信息:What issues should be considered when overriding equals and hashCode in Java?

【讨论】:

  • 所以,我使用各种系统输出对其进行了调试,并了解到我试图在对象的链表中查找对象的元素。因此,在检查所选元素的索引时,我需要该词的来源索引。有什么想法吗?
【解决方案2】:

仅通过您提供的代码很难判断,但如果 wordpar_list 仅包含 2 个元素(wordA 和 wordB),则 list.getSelectedValue() 的输出只能是 0 或 1。我会尝试打印出来(或debug) 来查看 list.getSelectedValue() 给你什么,但它可能不是你所期望的 0 或 1。

希望有帮助!

【讨论】:

  • wordpair_list 是一个链表,其中每个元素包含一个 wordA 和一个 wordB。链表类型是 WordPair,这是我创建的包含两个单词的类。
【解决方案3】:

这是一个很好的例子,您可以使用调试器。或者插入一堆 System.out.println() 打印出一些局部值。

【讨论】:

    【解决方案4】:

    首先你需要调试这段代码,打印出从indexOf()返回的任何内容 其次,您可能不应该使用LinkedList,而是使用ArrayList 作为最佳实践。

    另一件事,您可能必须将 wordpair_list.get(i) 转换为 WordPair 或任何该对象。

    【讨论】:

    • 当 JList 没有 indexof 操作时,如何更改 indexof 语句以获取 JList 'list' 的索引?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 2023-03-27
    • 2013-09-30
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多