【问题标题】:In java I'm getting an -Xlint:unchecked error在 java 中,我得到一个 -Xlint:unchecked 错误
【发布时间】:2014-03-28 22:16:34
【问题描述】:

这是我得到的错误:

J:\>javac -Xlint:unchecked Files.java
Files.java:58: warning: [unchecked] unchecked call to JList(E[]) as a member of
the raw type JList
        JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) {
                                           ^
  where E is a type-variable:
    E extends Object declared in class JList
1 warning

J:\>

这是给出错误的代码:

import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class Files
{
  public static void main(String [] args) throws IOException
  {
  String filename = "";
  String temp;
  boolean unique = true,found = false;
  ArrayList<String> passageWords = new ArrayList<String>();
  String temporary;
  String [] words;
  ArrayList<String> dictionaryWords = new ArrayList<String>();
  ArrayList<String> uniqueWords = new ArrayList<String>();
  filename = JOptionPane.showInputDialog(null,"Enter the name of the file you would like to display a unique list of words for","Filename input",1);
  File Passage = new File(filename);
  Scanner in = new Scanner(Passage);
    while(in.hasNext())
    {
        temp = in.nextLine();
        temp = temp.toLowerCase();
        temp = temp.replace("\\s+"," ");
        temp = temp.replace(".","");
        temp = temp.replace("\"","");
        temp = temp.replace("!","");
        temp = temp.replace("?","");
        temp = temp.replace(",","");
        temp = temp.replace(";","");
        temp = temp.replace(":","");
        temp = temp.replace("/","");
        temp = temp.replace("\\","");
        temp = temp.trim();
        words = temp.split(" ");
        for(int c = 0;c <words.length;c++)
        passageWords.add(words[c]);
    }
  File dictionary = new File("wordList.txt");
  Scanner input = new Scanner(dictionary);
    while(input.hasNext())
    {
        temporary = input.nextLine();
        dictionaryWords.add(temporary);
    }
    for(int counter = 0;counter<passageWords.size();counter++)
    {
    unique = true;  
        for(int count = 0;count<dictionaryWords.size();count++)
        {
        if((passageWords.get(counter).contentEquals(dictionaryWords.get(count))))
         unique = false;   
        }
    if(unique)
    uniqueWords.add(passageWords.get(counter));
    }
    JScrollPane pane = new JScrollPane(new JList(uniqueWords.toArray())) {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(200, 250);
                    }
                };;
      JOptionPane.showMessageDialog(null,pane,"Unique Words",1);
    for(int counts = 0;counts<uniqueWords.size();counts++)
    {
    for(int counters = 1;counters<uniqueWords.size();counters++)
    {
    if((uniqueWords.get(counts)).contentEquals(uniqueWords.get(counters)))
    {
    uniqueWords.remove(counters);
    counters--; 
    }   
    }   
    }
    JOptionPane.showMessageDialog(null,pane,"Unique Words",1);
  }
}

目前的代码正在读取两个文件,其中一个代表字典,另一个代表一段文本。它旨在检查字典中没有的单词并将其打印出来。代码中还有一些其他错误,但我只想先对其进行排序。

【问题讨论】:

    标签: java warnings compiler-warnings unchecked


    【解决方案1】:

    JList 是泛型类型,您将其用作原始类型。

    使用new JList&lt;Object&gt;(uniqueWords.toArray()),或者,如果您想要JList&lt;String&gt;,则更好:

    String[] wordsAsArray = uniqueWords.toArray(new String[uniqueWords.size()]);
    JScrollPane pane = new JScrollPane(new JList<String>(wordsAsArray));    
    

    请注意,您遇到的问题比代码中的问题更大。第一个是不正确的缩进,这使得它不可读。第二个是您在主线程中使用 Swing 组件,尽管它的文档非常清楚地表明它们只能从事件调度线程中访问。

    【讨论】:

    • 我知道缩进的问题,我会解决。我不知道你说的第二个问题是什么意思
    猜你喜欢
    • 2020-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-07
    相关资源
    最近更新 更多