java赋值​处理中的数组

6rqinv9w  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(226)
Table table;

    class AirPollution{
      String place;
      float NO2;
      float CO;
      int AP;

      AirPollution(String p,float x, float y, int c){
        place=p;
        NO2=x;
        CO=y;
        AP=c;
      }
    }

void loadData(){
  String[] data=loadStrings("airPollution.txt");
  AirPollution[] AP=new AirPollution[5];
  for(int i=0;i<data.length;i++){
    //Part I don't know
  }
}

没错,我在研究加工。下面是txt文件的内容

place,NO2(ppm),CO(ppm),Air pollution 
Nowon-gu,0.024,0.6,26
Dobong-gu,0.02,0.4,12
Seocho-gu,0.018,0.3,18
Gwanak-gu,0.022,0.5,20
Guro-gu,0.017,0.3,21

这个数据存在,我尝试输入值​​从第二行开始,第一行除外。
例如

AP[0].place="Nowon-gu"
AP[0].NO2=0.024
AP[0].CO=0.6
AP[0].AP=26
AP[1].place="Dobong-gu"

我希望这样。在这种情况下,我应该在不使用split函数的情况下输入每个函数吗?或者我应该用另一种方法?

eivgtgni

eivgtgni1#

除了ibz解释手动解析的详细答案(+1)之外,这里还有一个使用processing的table类的版本,可以使用 loadTable() :

Table table;

void setup() {
  // load table and specify the first row is a header
  table = loadTable("Airpollution.csv", "header");
  int rowCount = table.getRowCount();
  // for each row
  for (int i = 0; i < rowCount; i++) {
    // access the row
    TableRow row = table.getRow(i);
    // access each piece of data by column name...
    String place = row.getString("place");
    float no2 = row.getFloat("NO2(ppm)");
    float co = row.getFloat("CO(ppm)");
    // ...or column index
    int airPollution = row.getInt(3);
    // print the data
    println(i, place, no2, co, airPollution);
  }
}

既然您可以访问解析的数据,就可以将其插入到示例中 AirPollution :

Table table;

void setup() {
  // load table and specify the first row is the CSV header
  table = loadTable("Airpollution.csv", "header");
  int rowCount = table.getRowCount();
  // use row count (as .csv data could change)
  AirPollution[] AP = new AirPollution[rowCount];
  // for each row
  for (int i = 0; i < rowCount; i++) {
    // access the row
    TableRow row = table.getRow(i);
    // initialize AirPollution data with row data
    AP[i] = new AirPollution(row.getString("place"),
                             row.getFloat("NO2(ppm)"),
                             row.getFloat("CO(ppm)"),
                             row.getInt(3));
    // print the data
    println("row index",i,"data",AP[i]);
  }
}

class AirPollution {
  String place;
  float NO2;
  float CO;
  int AP;

  AirPollution(String p, float x, float y, int c) {
    place=p;
    NO2=x;
    CO=y;
    AP=c;
  }
  // display nicely when passing this instance to print/println
  String toString(){
    // %.3f = floating point value with 3 decimal places
    return String.format("{ place=%s, NO2=%.3f, CO=%.3f, AP=%d} ", place, NO2, CO, AP);
  }
}

请注意,我已将文件从.txt重命名为.csv:此扩展名可能有助于使用openoffice calc、excel、google sheets等预览/测试数据。
享受数据可视化的乐趣。如果你想了解更多关于 Table 类,而不是参考,也可以查看processing>examples>topics>advanced data>loadsavetable

hfwmuf9z

hfwmuf9z2#

我看没有理由不使用split。
在您的循环中,我首先在 , ,然后将每个部分添加到一个airpollution对象,该对象最终被添加到ap数组中。
注意,在我的解决方案中,我在索引1处初始化了for循环,而不是0。我在这里假设 loadStrings 生成您包含的输出,其中包含头行,我假设您实际上不希望ap数组中包含此头行。
请注意,现在您可能需要进行某种检查以确保 data.length 在进入for循环之前大于1。
请注意,我还将硬编码数组的大小从 5data.length - 1 . 这里的假设是数组大小不应该总是5,总是有一个标题行,并且您不希望在数组中包含标题行。
下面的代码应该完全按照您的要求执行,但请参阅代码块后面的我的注解以了解更多要点:

void loadData() {
    String[] data = loadStrings("airPollution.txt");
    AirPollution[] AP = new AirPollution[data.length - 1];
    for (int i = 1; i < data.length; i++) {
        String[] dataParts = data[i].split(",");
        AirPollution airPollution = new AirPollution(
            dataParts[0],
            Float.parseFloat(dataParts[1]),
            Float.parseFloat(dataParts[2]),
            Integer.parseInt(dataParts[3])
        );
        AP[i - 1] = airPollution;
    }
}

但是,从你的代码中,我看到 loadStrings 方法实际上没有在airpollution类中定义,您希望loadstrings返回void,而loadstrings方法不接受任何参数。这意味着,您可以用上面的代码填充ap数组,但是您的代码实际上不能用它做任何事情。
从代码上下文来看,我假设您希望loadstrings函数实际返回airpollution数组,以便您可以在 Package airpollution类的任何类中使用它。我将对上述代码稍加修改以反映这一点(添加return语句并更改return类型):

AirPollution[] loadData() {
    String[] data = loadStrings("airPollution.txt");
    AirPollution[] AP = new AirPollution[data.length - 1];
    for (int i = 1; i < data.length; i++) {
        String[] dataParts = data[i].split(",");
        AirPollution airPollution = new AirPollution(
            dataParts[0],
            Float.parseFloat(dataParts[1]),
            Float.parseFloat(dataParts[2]),
            Integer.parseInt(dataParts[3])
        );
        AP[i - 1] = airPollution;
    }

    return AP;
}

如果有什么不清楚的请告诉我!

相关问题