Java Eclipse 错误无法解析为类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19147047/
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 14:30:15  来源:igfitidea点击:

Eclipse errors cannot be resolved to a type

javaeclipseiteratorlinked-list

提问by Ryan Sisson

import java.awt.List;
import java.util.LinkedList;
import java.util.ListIterator;    

public class HW1 {
    public static void main(String[] args){    

        String s = "";
        boolean flag = false;
        int pos = -1;

        List roster = new LinkedList();   <--
        roster.add ( "alice" );
        roster.add ( "bob" );
        roster.add ( "chad" );
        roster.add ( "dan" );
        ListIterator<String> bookmark = roster.listIterator(); <--

        try {
            s = bookmark.next();
            s = bookmark.next();
            s = bookmark.next();
            s = bookmark.next();
            s = bookmark.next();
            flag = bookmark.hasNext();
            pos = bookmark.nextIndex();
        }catch ( Exception trouble ) {
            s = "Runtime Error generated";
        }

    }
}

This is considered a homework question, however I'm not looking for an answer. I'm getting multiple errors about resolving to a type, at the first error I get LinkedList and List cannot be resolved to a type. At the second arrow I get ListIterator cannot be resolved to a type.

这被认为是一个家庭作业问题,但我不是在寻找答案。我在解析为类型时遇到多个错误,在第一个错误中我得到 LinkedList 和 List 无法解析为类型。在第二个箭头处,我得到 ListIterator 无法解析为类型。

I am very new to Eclipse and we haven't done any code writing yet for this class so I am not used to the format Can anyone tell me how to resolve the errors I am getting

我对 Eclipse 非常陌生,我们还没有为这个类编写任何代码,所以我不习惯这种格式 谁能告诉我如何解决我遇到的错误

Updated* Same errors as before except I now have 3 more. List and LinkedList are a type mismatch, LinkedList is a raw type and references to a generic type need to be parameterized. then the second arrow I now get listIterator() is undefined for the type

更新* 与以前相同的错误,除了我现在还有 3 个。List 和 LinkedList 是类型不匹配,LinkedList 是原始类型,对泛型类型的引用需要参数化。那么我现在得到的第二个箭头 listIterator() 是未定义的类型

采纳答案by Matt Clark

List roster = new LinkedList();  

needs to become:

需要变成:

LinkedList roster = new LinkedList();  

回答by Hai

List is generic type, so you need to declare it as

List 是泛型类型,因此您需要将其声明为

List<String> myString = new LinkedList<String>();

回答by lem

Have you imported the libraries at the beginning of your program?

您是否在程序开始时导入了库?

import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

回答by Dai Nguyen-Van

I have the same problem and it take me a long time. Finally, I found that the lack of a close bracket "}". i think this issue is a syntax error.

我有同样的问题,需要很长时间。最后,我发现缺少一个右括号“}”。我认为这个问题是一个语法错误。