如何从 java 中的另一个 class 导入填充的现成 ArrayList?

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

我正在处理大量导入的数据,并在主要方法中使用 2 classes(WeatherStation, WeatherReading) 检索其中的某些部分。数据是气象站负载的温度读数(站号、名称、纬度、经度、年份、时间、温度等)我制作了第三个 class (SoloSiteIds),其唯一目的是 return 一个完整的站点 ID 的 ArrayList,没有复制。但是我无法将 ArrayList 从另一个 class 导入到我的主要方法中。我的 SoloSiteIds class 看起来像这样:

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(","); // 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);

只有顶部是相关的。我尝试了很多不同的 .set 和 .get 变体,使用“thisList”实例和一个新的 ArrayList,比如

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

等等等等。这可能看起来很愚蠢,但我只是在展示我已经尝试了很多东西,但我被卡住了 谢谢

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

据我了解您的情况,我认为 SoloSiteIds class 是不必要的。我将使用在您的 main class 中声明的 ArrayList 来解决您的问题,并使用也在您的 main class 中声明的 getSoleIds() 方法进行初始化。 getSoleIds() 方法应包含当前在 SoleSiteIds 初始值设定项中的代码。