【问题标题】:Access list from one method to another从一种方法到另一种方法的访问列表
【发布时间】:2020-06-16 08:59:31
【问题描述】:

在我的方法 loadData() 中,我从具有以下结构的文本文件中读取和存储数据:

1946-01-12;07:00:00;-1.3;G
1946-01-12;13:00:00;0.3;G
1946-01-12;18:00:00;-2.8;G
1946-01-13;07:00:00;-6.2;G
1946-01-13;13:00:00;-4.7;G
1946-01-13;18:00:00;-4.3;G

我将日期、时间和温度分别存储在变量 dateTime、Time 和 temperature 中。我想知道如何访问这些变量,例如dateTime 在另一种方法public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {} 中?我想在这个新方法中创建一个新列表,其值与dateTime 中的值相同,如下所示:List<LocalDate> list2 = Arrays.asList(dateTime); 有可能吗?

public class WeatherDataHandler {

  public WeatherDataHandler(LocalDate dateTime,LocalTime Time, double temperature, String tag) {
  }

  private static List<WeatherDataHandler> weatherData = new ArrayList<>();

  public void loadData(String filePath) throws IOException {
    //Read all data
    List<String> fileData = Files.readAllLines(Paths.get("filePath"));
    System.out.println(fileData);
        for(String str : fileData) {
        List<String> parsed = parseData(str);
        LocalDate dateTime = LocalDate.parse(parsed.get(0));
        LocalTime Time = LocalTime.parse(parsed.get(1));
        double temperature = Double.parseDouble(parsed.get(2));
        String tag = parsed.get(3);

        WeatherDataHandler weather = new WeatherDataHandler(dateTime, Time, temperature, tag);
        weatherData.add(weather);
        System.out.println(dateTime);}
    }

  private static List<String> parseData(String s) {
    return Arrays.asList(s.split(";"));

  }

  public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {

    return null;
  }
}

【问题讨论】:

    标签: java list variables methods


    【解决方案1】:

    由于您已将值保存到名为 weatherData 的静态字段中,因此您可以通过直接引用 weatherData 字段来访问它们。

    您创建一个新的 WeatherDataHandler 对象。但是,您不会将传递给构造函数的值保存在该对象中。如果您创建一个单独的类来保存天气的详细信息,您可能会发现会更容易。

    例如保存文件中一行的类:

    public class Weather {
    
        private LocalDate dateTime;
        private LocalTime time;
        private double temperature;
        private String tag;
    
        public Weather(LocalDate dateTime, LocalTime time, double temperature, String tag) {
            this.dateTime = dateTime;
            this.time = time;
            this.temperature = temperature;
            this.tag = tag;
        }
    
        public LocalDate getDateTime() {
            return dateTime;
        }
    
        public void setDateTime(LocalDate dateTime) {
            this.dateTime = dateTime;
        }
    
        public LocalTime getTime() {
            return time;
        }
    
        public void setTime(LocalTime time) {
            this.time = time;
        }
    
        public double getTemperature() {
            return temperature;
        }
    
        public void setTemperature(double temperature) {
            this.temperature = temperature;
        }
    
        public String getTag() {
            return tag;
        }
    
        public void setTag(String tag) {
            this.tag = tag;
        }
    }
    

    更改 WeatherDataHandler 以使用 Wea​​ther 类:

    public class WeatherDataHandler {
    
        // You probably don't need this to be static
        private List<Weather> weatherData = new ArrayList<>();
    
        public void loadData(String filePath) throws IOException {
            //Read all data
            List<String> fileData = Files.readAllLines(Paths.get(filePath));
            System.out.println(fileData);
            for(String str : fileData) {
                List<String> parsed = parseData(str);
                LocalDate dateTime = LocalDate.parse(parsed.get(0));
                LocalTime Time = LocalTime.parse(parsed.get(1));
                double temperature = Double.parseDouble(parsed.get(2));
                String tag = parsed.get(3);
    
                Weather weather = new Weather(dateTime, Time, temperature, tag);
                weatherData.add(weather);
                System.out.println(dateTime);}
        }
    
        // You probably don't need this to be static
        private List<String> parseData(String s) {
            return Arrays.asList(s.split(";"));
    
        }
    
        public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {
            // access the weatherData directly
            List<String> values = new ArrayList<>();
            for (Weather weather : weatherData) {
                values.add(weather.getDateTime().toString());
            }
            return values;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多