简体   繁体   English

Java:错误“无法访问LinkedList”

[英]Java: error “Can not access LinkedList”

I am writing a simple Java program to rotate a Linked List, but when trying to compile the Java code through the compiler via: javac RotateLinkedList.java , I get the following error in my console: 我正在编写一个简单的Java程序来旋转链表,但是当尝试通过javac RotateLinkedList.java通过编译器编译Java代码时,在控制台中出现以下错误:

jared@jared-linux:~/Desktop/Code Interviews$ javac RotateLinkedList.java 
./LinkedList.java:9: error: class ListOperations is public, should be declared in a file named ListOperations.java
public class ListOperations{
       ^
RotateLinkedList.java:82: error: cannot access LinkedList
        LinkedList<Integer> ll = new LinkedList<Integer>();
        ^
bad source file: ./LinkedList.java
    file does not contain class LinkedList
    Please remove or make sure it appears in the correct subdirectory of the sourcepath.

Which is odd as this looks to be that the source code for Java is what is failing? 奇怪的是,这似乎是Java的源代码出了什么问题? I am compiling against Java JDK 8 and on Ubuntu 14.04 . 我正在针对Java JDK 8Ubuntu 14.04编译。

Below is a snippet of the code I am running to create the LinkedList, if that helps: 以下是我正在创建LinkedList的代码片段,如果有帮助的话:

import java.util.*;
public class RotateLinkedList {
    public static void main(String args[]) {
        // Crate the linked List
        LinkedList<Integer> ll = new LinkedList<Integer>();
        // Adding elements to linked list
        System.out.println("How many elements should be in the binary linked list: ");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        if((num != null) && (num > 0)) {
            System.out.println("Adding " + num + " elements into the linked list.");
            for(int i = 0; i < num; i++) {
                ll.add(i);
            }
            System.out.println("Linked List: " + ll);
        } else { 
            System.out.println("NULL or invalid number inputted. Aborting");
        }
    }
}

Appreciate any feedback! 感谢任何反馈! Cheers. 干杯。

The compilation error message is quite straight forward: The name of your java file is not the same as that of the public class defined in it. 编译错误消息很简单:Java文件的名称与其中定义的公共类的名称不同。

It is one of the basic constructs of Java language that the file name should match exactly with the publicly declared class in that java file. 文件名应与该Java文件中公开声明的类完全匹配,这是Java语言的基本构造之一。 Note here that there could be multiple classes in a java file, nevertheless, only one publicly declared class in it! 请注意,在一个Java文件中可能有多个类,但是其中只有一个公开声明的类!

Just to confirm, try changing the name of your java file to "ListOperations.java" and then compile it. 只是为了确认,请尝试将Java文件的名称更改为“ ListOperations.java”,然后进行编译。

So I was an idiot and forgot I had a LinkedList.java (which is mostly psuedocode, etc) file in my source folder when compiling, so the compiler wanted to import that file when compiling RotateLinkedList.java . 所以我是个白痴,忘记了在编译时在我的源文件夹中有一个LinkedList.java (主要是psuedocode等),因此编译器想在编译RotateLinkedList.java时导入该文件。 Argh! 啊! Sorry about that. 对于那个很抱歉。 Appreciate the views / responses, though! 欣赏意见/回应,不过!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM