Search code examples
javaclassarraylistgetter-setter

How do I import a populated ready-made ArrayList from another class in java?


I'm working with a large set of imported data and retrieving certain parts of it in the main method with 2 classes(WeatherStation, WeatherReading).The data is temperature readings at loads of weather stations(station id, name, lat, lon, year, time, temp etc) I made a third class (SoloSiteIds) whose sole purpose was to return a whole and complete ArrayList of the site ids with no duplication. But I cannot import the ArrayList from the other class into my main method. My SoloSiteIds class looks like this:

public class SoloSiteIds {

 static ArrayList <Integer> siteIds = new ArrayList <Integer>();
 

 public SoloSiteIds() {
 }
 
 public SoloSiteIds( ArrayList <Integer> siteIds) {
     
     String[] weatherData = WeatherData.getData();{ // get the weather data 
     
         for (int i = 1; i < weatherData.length; i++) {
                String line = weatherData[i];
                String[] elements = line.split(","); // Split the data at ",
                
                String siteid = elements[0]; // convert all the site id's at index 0 to integers
                int id = Integer.parseInt(siteid);
                if(!siteIds.contains(id)) {
                    siteIds.add(id);
                }
     
     
     this.siteIds=siteIds;



         }
    
}
}

public static ArrayList<Integer> getSiteIds() {
    return siteIds;
}

public ArrayList<Integer> setSiteIds(ArrayList<Integer> siteIds) {
    return this.siteIds = siteIds;
}   

}

The main method where I am trying to import the ArrayList "siteIds" looks like this:

        WeatherStation thisStation = new WeatherStation (id, name, lat, lon);
    WeatherReading thisReading = new WeatherReading(year, month, date, hour, windSpeed, temp);
    SoloSiteIds siteList= new SoloSiteIds();
    


    
    String[] weatherData = WeatherData.getData();{ // get the weather data

        
        for (int i = 1; i < weatherData.length; i++) {
            String line = weatherData[i];
            String[] elements = line.split(","); // Split the data at ","   
            
    
            
                

                    String siteid = elements[0]; // convert all the site id's at index 0 to integers
                    id = Integer.parseInt(siteid);
                    thisStation.setId(id);
                    
                    thisStation.setName(elements[1]);
                    
                    //parse the different elements into different data types
                    
                    String stringLat = elements[2];    
                    lat= Double.parseDouble(stringLat);
                    lat = thisStation.setLat(lat);
                    lat=thisStation.setLat(lat);
                        
                    String stringLon = elements[3];
                    lon= Double.parseDouble(stringLon);
                    lat = thisStation.setLon(lon);
                    lat=thisStation.setLon(lon);
                    
                    String stringTemp=elements[9];
                    temp=Double.parseDouble(stringTemp);
                    temp=thisReading.setTemp(temp);

Only the top part is relevant. I have tried lots of different variation of .set and .get using "thisList" instance and a new ArrayList like

ArrayList<Integer> siteIds = thisList.setSiteIds();
ArrayList<Integer> siteIds= SoloSiteIds.getSiteIds();
thisList=Siteids.setSiteIds();
thisList=SingleSoloSites.setSiteIds();

etc etc. This might look stupid but im just showing Ive tried numerous things and i am stuck Thanks


Solution

  • I believe your problem is that you are initializing siteIds as an empty Arry list but you are not setting the data in a static way (the set Method is not static).

    As far as I am aware of your situation, I belive that the SoloSiteIds class is unnescessary. I would solve your problem with an ArrayList declared in your main class and initialize with a getSoleIds() method also declared in your main class. The getSoleIds() Method should contain the code currently in the SoleSiteIds initializer.