使用欄位型別進行操作
你正在為一家科技訓練新創建立產品型錄。在建立課程之前,你需要先分析產品定價—了解價格區間有助於決定應優先投入哪些產品。使用像 DoubleColumn 這樣的型別化欄位,能提供一般欄位沒有的統計方法。
Table 與 DoubleColumn 類別已為你匯入。
本練習屬於課程
Java 中的資料匯入
練習說明
- 將
"products.csv"讀入為products表格。 - 取得
"Price"欄位並存入prices。 - 列印平均價格。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
public class ProductCatalog {
public static void main(String[] args) {
try {
// Load products.csv into the products table
Table products = Table.____().csv("____");
// Access the Price column and store it in prices
DoubleColumn ____ = products.doubleColumn("____");
// Print the mean price
System.out.println("Price Stats - Min: $" + prices.min() +
", Max: $" + prices.max() +
", Mean: $" + prices.____());
System.out.println(products.first(5));
} catch (Exception e) {
System.err.println("Error reading CSV files: " + e.getMessage());
}
}
}