开始使用免费开始使用

修改表结构

气象站现在需要更新其天气数据表,以容纳新的温度预测。他们想在保留实际记录温度以便对比的同时,加入预测温度。

已为您导入 Tablesaw 的 TableDoubleColumnStringColumn 类。

本练习是课程的一部分

Java 中的数据导入

查看课程

练习说明

  • 将 "Temperature" 列重命名为 "ActualTemp"。
  • 向表中添加一个新的 "ForecastTemp" 列。
  • 打印表结构。
  • 打印前 3 行。

交互式实操练习

通过完成这段示例代码来试试这个练习。

public class ModifyWeatherTable {
    public static void main(String[] args) {
        StringColumn days = StringColumn.create("Day", 
            "Monday", "Tuesday", "Wednesday");
            
        DoubleColumn temperatures = DoubleColumn.create("Temperature", 
            22.5, 24.0, 23.2);
            
        DoubleColumn precipitation = DoubleColumn.create("Precipitation", 
            0.0, 2.5, 5.2);
            
        Table weatherData = Table.create("WeatherData")
            .addColumns(days, temperatures, precipitation);
        
        // Rename the Temperature column
        weatherData.____("Temperature").____("____");
        
        // Add a new ForecastTemp column
        DoubleColumn forecastTemps = DoubleColumn.create("ForecastTemp", 
            23.0, 25.2, 22.8);
        weatherData = weatherData.____(forecastTemps);
        
        // Print the table structure
        System.out.println(weatherData.____());
        
        // Print the first three rows
        System.out.println(weatherData.____(____));
        
    }
}
编辑并运行代码