如何从Java中的另一个类导入已填充的现成ArrayList?

问题描述

我正在处理大量导入的数据,并通过2类(WeatherStation,WeatherReading)的主要方法检索其中的某些部分。数据是气象站负载(站号,名称,纬度)的温度读数,lon,年,时间,温度等),我制作了第三类(SoloSiteIds),其唯一目的是返回一个完整且完整的站点ID ArrayList,而不重复。但是我不能将ArrayList从另一个类导入到我的main方法中。我的SoloSiteIds类如下:

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

}

我尝试导入ArrayList“ siteIds”的主要方法如下:

        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(","   
            
    
            
                

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

仅顶部相关。我尝试使用“ thisList”实例和新的ArrayList之类的.set和.get进行许多不同的变化

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

等等等。这可能看起来很愚蠢,但我只是表明我已经尝试了很多事情而被困住了 谢谢

解决方法

我相信您的问题是,您正在将siteIds初始化为一个空的Arry列表,但不是以静态方式设置数据(set方法不是静态的)。

据我了解您的情况,我相信SoloSiteIds类是不必要的。我将通过在主类中声明的ArrayList解决问题,并使用也在主类中声明的getSoleIds()方法进行初始化。 getSoleIds()方法应包含SoleSiteIds初始化程序中当前的代码。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...