You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java GUI程序开发疑问:CSV数据存储与多维数组操作

Java GUI Weather Data App: Guidance on Your Questions

Hey there, let's break down your three key questions to get you moving in the right direction:

1. Storing Data in a Multi-Dimensional Array & Performing Operations

Your current approach uses separate 1D arrays for each column, which works, but a 2D array or custom data class will make operations cleaner:

  • 2D Array Option: Since you have 804 rows and 7 columns, declare a double[][] data = new double[804][7]; Then, in your read loop, populate it directly:
    int index = 0;
    while ((line = file.readLine()) != null) {
        String[] splits = line.split(",");
        for (int col = 0; col < 7; col++) {
            data[index][col] = Double.parseDouble(splits[col]);
        }
        index++;
    }
    
    For calculations (e.g., column average), loop through all rows for the target column:
    public double getColumnAverage(double[][] data, int columnIndex) {
        double total = 0;
        for (double[] row : data) {
            total += row[columnIndex];
        }
        return total / data.length;
    }
    
  • Custom Data Class Option: For better readability (instead of remembering column indices), create a WeatherEntry class with fields like year, month, tmax, etc. Then store all entries in a List<WeatherEntry>. Calculations would involve iterating over the list and summing specific fields.

2. GUI Development Priority

Focus on building and validating core logic first, then wrapping it in GUI:

  • Step 1: Finalize your CSV loading and calculation logic in a command-line (CLI) test. Make sure you can successfully load the file, compute averages/min/max without any GUI code—this avoids debugging two layers at once.
  • Step 2: Build a minimal GUI:
    • Add a "Load CSV" button that lets users select the file (use JFileChooser), loads the data, and shows a confirmation (e.g., "Loaded 804 rows" in a label).
    • Add buttons for "Calculate Averages" and "Calculate Min/Max" that trigger your existing calculation logic and display results in a text area or labels.
  • Step 3: Tackle the chart feature once the core GUI and data operations are stable.

3. Class Structure & GUI Integration

Avoid creating a separate class for every tiny function—instead, use separation of concerns to keep code organized:

  • Data Model Class: Create a WeatherDataset class that holds your loaded data (either 2D array or List<WeatherEntry>) and exposes methods like getAllAverages(), getColumnMin(int column), getColumnMax(int column). This encapsulates all data-related operations.
  • CSV Loader Class: A CsvDataLoader class with a static method like loadCsv(String filePath) that returns a WeatherDataset. This keeps file-reading logic separate from business logic and GUI.
  • GUI Class: A single WeatherAppGui class (using Swing/JavaFX) that handles all UI elements. When a button is clicked, it calls CsvDataLoader to get data, then uses WeatherDataset to compute results, and updates the UI with those results.

This structure makes your code easier to test, maintain, and modify later.

内容的提问来源于stack exchange,提问作者rflwill

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:22:00