I'm trying to build an ArrayList of Earthquake objects but Java is complaining about not being visible.

My Code:

import java.io.*;import java.util.Arrays.*;public class ObservatoryTest {private ArrayList <Earthquakes> listOfEarthquakes; //This list is not visibleEarthquakes 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);}}}

My aim is to build a list of Earthquake objects. Could somebody tell me why and how to fix my code? Thanks

--------------edit--------------------

The error message is the type ArrayList is not visible however changing visibility modifier to public doesn't have any effect.

4

Best Answer


For some reason, you've used a type-import-on-demand declaration for nested member of Arrays

import java.util.Arrays.*;

In its current implementation, Arrays declares a private nested type named ArrayList. That's not visible to your code since it's private.

You meant to import java.util.ArrayList.

Missing the below import statement

 import java.util.ArrayList;public class ObservatoryTest {private ArrayList <Earthquakes> listOfEarthquakes; //This list is not visibleEarthquakes 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);}}

You are missing the import statement for ArrayList

import java.util.ArrayList;

To solve such problems call the "Organize imports" from you SDE. e.g in Eclipse: Ctrl-Shift-O

You need to initialize the list before adding the elements to it !

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