0

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 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);
        }

    }
}

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.

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106

4 Answers4

7

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.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
3

Missing the below import statement

 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);
        }

    }
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
RamPrakash
  • 1,687
  • 3
  • 20
  • 25
1

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

AlexWien
  • 28,470
  • 6
  • 53
  • 83
-1

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

listOfEarthquakes = new ArrayList<Earthquakes>();
listOfEarthquakes.add(myEarthquake);
kausDix75
  • 36
  • 5
  • By the way - when I had posted my answer, the "---edit---" part was not there - so it was not clear what the exact error was ! and anyways this list was not initialized anyways - at least in the posted code. So I'm a little surprised by the downvote ! – kausDix75 Oct 30 '15 at 17:05
  • I had not downvoted, However, if finally, even after an edit of the original question, the answer is wrong. It is ok that finally it is downvoted, because it is a wrong answer. You can remove the answer, then your reputation points, you lost, will be returned to you – AlexWien Nov 02 '15 at 14:19