变量在基本Java类中不可见

时间:2015-10-29 17:09:11

标签: java

我正在尝试构建Earthquake个对象的ArrayList,但Java抱怨不可见。

我的代码:

import java.io.*;
import java.util.Arrays.*;

public class ObservatoryTest {
    private ArrayList <Earthquakes> listOfEarthquakes; //This list is not visible
    Earthquakes Quake1 = new Earthquakes(4.5,"cheese",1995);
    Earthquakes Quake2 = new Earthquakes(6.5,"fish",1945);
    Earthquakes Quake3 = new Earthquakes(10.5,"Chorizo",2015);

    public void buildList(Earthquakes... args){
        for(Earthquakes myEarthquake : args){
            listOfEarthquakes.add(myEarthquake);
        }

    }
}

我的目标是建立一个地震对象清单。有人可以告诉我为什么以及如何修复我的代码?感谢

--------------编辑--------------------

错误消息为the type ArrayList is not visible但是,将公开广告素材修改程序更改为公开信息并不会产生任何影响。

4 个答案:

答案 0 :(得分:7)

出于某种原因,您使用type-import-on-demand declaration作为Arrays的嵌套成员

import java.util.Arrays.*;

在当前的实现中,Arrays声明了一个名为private的{​​{1}}嵌套类型。由于它是ArrayList,因此您的代码无法看到。

您打算导入private

答案 1 :(得分:3)

缺少以下导入声明

 import java.util.ArrayList;

 public class ObservatoryTest {

 private ArrayList <Earthquakes> listOfEarthquakes; //This list is not visible
    Earthquakes Quake1 = new Earthquakes(4.5,"cheese",1995);
    Earthquakes Quake2 = new Earthquakes(6.5,"fish",1945);
    Earthquakes Quake3 = new Earthquakes(10.5,"Chorizo",2015);

    public void buildList(Earthquakes... args){
        for(Earthquakes myEarthquake : args){
            listOfEarthquakes.add(myEarthquake);
        }

    }

答案 2 :(得分:1)

您缺少ArrayList

的导入声明
import java.util.ArrayList;

要解决此类问题,请从您的SDE中调用“组织导入”。例如在Eclipse中:Ctrl-Shift-O

答案 3 :(得分:-1)

您需要在向其添加元素之前初始化列表!

listOfEarthquakes = new ArrayList<Earthquakes>();
listOfEarthquakes.add(myEarthquake);