You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse中LinkedList无法正确导入的问题求助

Eclipse中LinkedList无法解析为类型的解决方法

报错信息

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    LinkedList cannot be resolved to a type
    LinkedList cannot be resolved to a type
    LinkedList cannot be resolved to a type

    at StacksQLLExample.main(StacksQLLExample.java:23)

问题代码

import java.util.ListIterator;
import java.util.Queue;
import java.util.Stack;
import java.util.LinkedList;

public class StacksQLLExample
{
    public static void main(String[] args)
    {
        Stack<Integer> s = new Stack<Integer>();
        s.push(1);
        s.push(5);
        s.push(52);
        s.push(Integer.MAX_VALUE);
        System.out.println(s);
        while (!s.isEmpty())
        {
            System.out.println("peek: "+ s.peek());
            //System.out.println(s);
            System.out.println("pop: "+ s.pop());
        }

        Queue<Integer> q = new LinkedList<Integer>();
        q.add(1);
        q.add(5);
        q.add(52);
        q.add(Integer.MAX_VALUE);
        System.out.println(q);
        while (!s.isEmpty())
        {
            System.out.println("peek: "+ q.peek());
            System.out.println("delete: "+ q.remove()) ;
        }
        System.out.println();

        LinkedList<String> ll = new LinkedList<String>();
        ll.addFirst("D");
        ll.addLast("H");
        ll.add("R"); // same as addLast
        ll.addLast("T");

        System.out.println(ll);
        ListIterator<String> iter = ll.listIterator(); // puts iter at the beginning of the list
        while (iter.hasNext())
        {
            String str = iter.next(); // returns what is to the left of the iter
            str += str;
            System.out.println(str);
        }
        if (iter.hasNext()) iter.next(); // iter is at the end of the list
        iter = ll.listIterator(); // back to the beginning

        while (iter.hasNext())
        {
            String str = iter.next();

            if (str.equals("D"))
            {
                iter.add("J");
            }

        }
        System.out.println(ll);
        iter.remove();
        System.out.println(ll);
        iter.add("K");
        System.out.println(ll);
        // This creates an error because the iter never moved over with the other element
        // You also can't remove what you just added
        // iter.remove();
        // System.out.println(iter); // prints a memory reference
    }
}

解决步骤

  • 清理重建项目
    打开Eclipse菜单栏,选择Project -> Clean...,选中当前项目后点击OK完成清理。右键项目选择Build Project重新编译,这类缓存导致的编译错误大多能通过此方法解决。

  • 检查JRE系统库配置
    右键项目 -> Build Path -> Configure Build Path,切换到Libraries标签页:

    • 若JRE System Library显示感叹号,说明路径无效,点击Edit选择本地正确的JDK/JRE路径(确保版本为Java 5及以上,LinkedList泛型从Java 5开始支持)。
    • 确认使用的是标准JRE,避免自定义的残缺JRE导致类无法识别。
  • 对齐Java编译版本
    在Build Path的Java Compiler标签页,确保Compiler compliance level与JRE版本一致(比如JRE 8对应1.8),版本不匹配会导致编译时无法识别JDK类。

  • 排查类名冲突与导入错误

    • 确认导入语句import java.util.LinkedList;无大小写拼写错误。
    • 检查项目中是否存在自定义的LinkedList类,若有则重命名该类,避免与JDK自带类冲突。右键代码中的LinkedList选择Open Declaration,验证是否跳转到java.util.LinkedList类。

内容的提问来源于stack exchange,提问作者Matthew Gligore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 02:55:59