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

huangapple go评论103阅读模式
英文:

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

问题

我正在使用大量导入的数据并在主方法中使用两个类(WeatherStation、WeatherReading)检索其中的某些部分。数据是许多气象站的温度读数(站点ID、名称、纬度、经度、年份、时间、温度等)。我创建了第三个类(SoloSiteIds),其唯一目的是返回一个完整的站点ID的ArrayList,不包含重复项。但是,我无法将ArrayList从其他类导入到我的主方法中。我的SoloSiteIds类如下:

  1. public class SoloSiteIds {
  2. static ArrayList<Integer> siteIds = new ArrayList<Integer>();
  3. public SoloSiteIds() {
  4. }
  5. public SoloSiteIds(ArrayList<Integer> siteIds) {
  6. String[] weatherData = WeatherData.getData(); // 获取气象数据
  7. for (int i = 1; i < weatherData.length; i++) {
  8. String line = weatherData[i];
  9. String[] elements = line.split(",");
  10. String siteid = elements[0];
  11. int id = Integer.parseInt(siteid);
  12. if (!siteIds.contains(id)) {
  13. siteIds.add(id);
  14. }
  15. }
  16. this.siteIds = siteIds;
  17. }
  18. public static ArrayList<Integer> getSiteIds() {
  19. return siteIds;
  20. }
  21. public ArrayList<Integer> setSiteIds(ArrayList<Integer> siteIds) {
  22. return this.siteIds = siteIds;
  23. }
  24. }

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

  1. WeatherStation thisStation = new WeatherStation(id, name, lat, lon);
  2. WeatherReading thisReading = new WeatherReading(year, month, date, hour, windSpeed, temp);
  3. SoloSiteIds siteList = new SoloSiteIds();
  4. String[] weatherData = WeatherData.getData();
  5. for (int i = 1; i < weatherData.length; i++) {
  6. String line = weatherData[i];
  7. String[] elements = line.split(",");
  8. String siteid = elements[0];
  9. id = Integer.parseInt(siteid);
  10. thisStation.setId(id);
  11. thisStation.setName(elements[1]);
  12. String stringLat = elements[2];
  13. lat = Double.parseDouble(stringLat);
  14. lat = thisStation.setLat(lat);
  15. lat = thisStation.setLat(lat);
  16. String stringLon = elements[3];
  17. lon = Double.parseDouble(stringLon);
  18. lat = thisStation.setLon(lon);
  19. lat = thisStation.setLon(lon);
  20. String stringTemp = elements[9];
  21. temp = Double.parseDouble(stringTemp);
  22. temp = thisReading.setTemp(temp);
  23. }

请注意,上面的代码示例中只有顶部部分与导入ArrayList "siteIds" 有关。您可以尝试使用以下方式来获取ArrayList "siteIds" 的值:

  1. 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:

  1. public class SoloSiteIds {
  2. static ArrayList &lt;Integer&gt; siteIds = new ArrayList &lt;Integer&gt;();
  3. public SoloSiteIds() {
  4. }
  5. public SoloSiteIds( ArrayList &lt;Integer&gt; siteIds) {
  6. String[] weatherData = WeatherData.getData();{ // get the weather data
  7. for (int i = 1; i &lt; weatherData.length; i++) {
  8. String line = weatherData[i];
  9. String[] elements = line.split(&quot;,&quot;); // Split the data at &quot;,
  10. String siteid = elements[0]; // convert all the site id&#39;s at index 0 to integers
  11. int id = Integer.parseInt(siteid);
  12. if(!siteIds.contains(id)) {
  13. siteIds.add(id);
  14. }
  15. this.siteIds=siteIds;
  16. }
  17. }
  18. }
  19. public static ArrayList&lt;Integer&gt; getSiteIds() {
  20. return siteIds;
  21. }
  22. public ArrayList&lt;Integer&gt; setSiteIds(ArrayList&lt;Integer&gt; siteIds) {
  23. return this.siteIds = siteIds;
  24. }

}

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

  1. WeatherStation thisStation = new WeatherStation (id, name, lat, lon);
  2. WeatherReading thisReading = new WeatherReading(year, month, date, hour, windSpeed, temp);
  3. SoloSiteIds siteList= new SoloSiteIds();
  4. String[] weatherData = WeatherData.getData();{ // get the weather data
  5. for (int i = 1; i &lt; weatherData.length; i++) {
  6. String line = weatherData[i];
  7. String[] elements = line.split(&quot;,&quot;); // Split the data at &quot;,&quot;
  8. String siteid = elements[0]; // convert all the site id&#39;s at index 0 to integers
  9. id = Integer.parseInt(siteid);
  10. thisStation.setId(id);
  11. thisStation.setName(elements[1]);
  12. //parse the different elements into different data types
  13. String stringLat = elements[2];
  14. lat= Double.parseDouble(stringLat);
  15. lat = thisStation.setLat(lat);
  16. lat=thisStation.setLat(lat);
  17. String stringLon = elements[3];
  18. lon= Double.parseDouble(stringLon);
  19. lat = thisStation.setLon(lon);
  20. lat=thisStation.setLon(lon);
  21. String stringTemp=elements[9];
  22. temp=Double.parseDouble(stringTemp);
  23. 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

  1. ArrayList&lt;Integer&gt; siteIds = thisList.setSiteIds();
  2. ArrayList&lt;Integer&gt; siteIds= SoloSiteIds.getSiteIds();
  3. thisList=Siteids.setSiteIds();
  4. 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.

huangapple
  • 本文由 发表于 2020年8月11日 02:54:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定