英文:
How do I import a populated ready-made ArrayList from another class in java?
问题
我正在使用大量导入的数据并在主方法中使用两个类(WeatherStation、WeatherReading)检索其中的某些部分。数据是许多气象站的温度读数(站点ID、名称、纬度、经度、年份、时间、温度等)。我创建了第三个类(SoloSiteIds),其唯一目的是返回一个完整的站点ID的ArrayList,不包含重复项。但是,我无法将ArrayList从其他类导入到我的主方法中。我的SoloSiteIds类如下:
public class SoloSiteIds {
static ArrayList<Integer> siteIds = new ArrayList<Integer>();
public SoloSiteIds() {
}
public SoloSiteIds(ArrayList<Integer> siteIds) {
String[] weatherData = WeatherData.getData(); // 获取气象数据
for (int i = 1; i < weatherData.length; i++) {
String line = weatherData[i];
String[] elements = line.split(",");
String siteid = elements[0];
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();
for (int i = 1; i < weatherData.length; i++) {
String line = weatherData[i];
String[] elements = line.split(",");
String siteid = elements[0];
id = Integer.parseInt(siteid);
thisStation.setId(id);
thisStation.setName(elements[1]);
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);
}
请注意,上面的代码示例中只有顶部部分与导入ArrayList "siteIds" 有关。您可以尝试使用以下方式来获取ArrayList "siteIds" 的值:
ArrayList<Integer> siteIds = SoloSiteIds.getSiteIds();
这应该能够帮助您在主方法中获取导入的站点ID列表。
英文:
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
答案1
得分: 1
我认为你的问题是你将siteIds初始化为一个空的ArrayList,但你没有以静态方式设置数据(set方法不是静态的)。
据我所知,根据你的情况,我认为SoloSiteIds类是不必要的。我会使用在你的主类中声明的ArrayList来解决你的问题,并使用在主类中也声明的getSoleIds()方法进行初始化。
getSoleIds()方法应该包含当前在SoleSiteIds初始化程序中的代码。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论