访问列表从一个方法到另一个方法。

huangapple 未分类评论64阅读模式
标题翻译

Access list from one method to another

问题

在我的方法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

我将日期、时间和温度分别存储在变量dateTimeTimetemperature中。我想知道如何在另一个方法public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {}中访问这些变量,例如dateTime。我想在这个新方法中创建一个与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 {
        // 读取所有数据
        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;
    }
}

以上。

英文翻译

In my method loadData() I read and store data from a textfile with the following structure:

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

I store date, time and temperature in the variables dateTime, Time and temperature respectively. I wonder how I can access these variables, for example dateTime in another method public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {}? I want to create a new list in this new method with same values as in dateTime like this: List<LocalDate> list2 = Arrays.asList(dateTime); Is it possible?

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

答案1

得分: 0

由于你保存了值到一个名为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;
    }

    // 省略其他getter和setter方法
}

并且对WeatherDataHandler进行更改以使用Weather类:

public class WeatherDataHandler {
    
    private List<Weather> weatherData = new ArrayList<>();

    public void loadData(String filePath) throws IOException {
        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);
        }
    }

    // 省略其他方法

    public List<String> missingValues(LocalDate dateFrom, LocalDate dateTo) {
        List<String> values = new ArrayList<>();
        for (Weather weather : weatherData) {
            values.add(weather.getDateTime().toString());
        }
        return values;
    }
}
英文翻译

As you've saved values to a static field called weatherData, you can simply access them by referring to weatherData field directly.

You create a new WeatherDataHandler object. However, you don't save the values passed to the constructor in that object. You might find it easier if you create a separate class to hold details of the Weather.

e.g. Class to hold one row from the file:

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

And changes to the WeatherDataHandler to use the Weather class:

public class WeatherDataHandler {

    // You probably don&#39;t need this to be static
    private List&lt;Weather&gt; weatherData = new ArrayList&lt;&gt;();

    public void loadData(String filePath) throws IOException {
        //Read all data
        List&lt;String&gt; fileData = Files.readAllLines(Paths.get(filePath));
        System.out.println(fileData);
        for(String str : fileData) {
            List&lt;String&gt; 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&#39;t need this to be static
    private List&lt;String&gt; parseData(String s) {
        return Arrays.asList(s.split(&quot;;&quot;));

    }

    public List&lt;String&gt; missingValues(LocalDate dateFrom, LocalDate dateTo) {
        // access the weatherData directly
        List&lt;String&gt; values = new ArrayList&lt;&gt;();
        for (Weather weather : weatherData) {
            values.add(weather.getDateTime().toString());
        }
        return values;
    }
}

huangapple
  • 本文由 发表于 2020年3月4日 06:21:34
  • 转载请务必保留本文链接:https://java.coder-hub.com/60516385.html
匿名

发表评论

匿名网友

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

确定