0

如果我的示例数据是一个如下所示的 CSV 文件:

a,,,b,
36,28,90,5,24

拥有类似的东西的最好方法是什么

myStringArray = [a, [36,28,90]], [b, [5,24]]

或者

myStringArray1 = {a,b}; // 这部分我可以做
myStringArray2 = {{36,28,90}, {5,24}};

我正在使用基于 Java 的处理,但我的问题更多是关于 Java 的一般功能,所以请原谅我的代码格式,如果它看起来不正确的话。

这是我的导入类:

class dTable {

  int rowCount;
  int columnCount;
  String[][] data;
  String filename;

  dTable(String _filename) {
    filename = _filename;
  }

  void load() {
    String[] rows = loadStrings(filename);
    String[] columns = split(rows[0], ',');
    rowCount = rows.length;
    columnCount = columns.length;

    //set the size of the matrix
    data = new String[rows.length][columns.length]; 

    // add column pieces into data matrix
    for (int i = 0; i < rows.length; i++) {
      String[] colEntries = split(rows[i], ',');
      for (int j = 0; j < columns.length; j++) {
        data[i][j] = colEntries[j];
      }
    }


  }
}

这是我在解析课上的失败尝试:

class dParse {

  String[] bMaj;
  String[][] bMin;

  dParse() {
  }

  void getList(int integer) {

    ArrayList dMaj = new ArrayList();
    ArrayList dMin = new ArrayList();

    String[][] minimums;

    String[] _rowNameMaj = table.data[0]; //get data first to match
    String[] _rowName = table.data[integer]; // get data to fill

    //get first variables
    for (int i = 0; i<_rowName.length; i++) { //iterate over 
      ArrayList tempMin = new ArrayList();
      if (trim(_rowNameMaj[i]).length() != 0) {
        dMaj.add(_rowNameMaj[i]);
      }
    }

    //place maj values from arraylist into an array
    String[] _bMaj = (String[]) dMaj.toArray(new String[0]);
    bMaj = _bMaj; //set value to global variable


    //min values
    ArrayList bigOne = new ArrayList();
    int count = 0;
    for (int i = 0; i<_rowName.length; i++) { //iterate over 
      ArrayList tempMin = new ArrayList();
      if (trim(_rowNameMaj[i]).length() != 0) { //check if box is not empty & set count to 0
        //tempMin = dMaj.get(i); // set inner list
        //println("maj " + count + " " + _rowNameMaj[i]);
        tempMin.add(_rowName[i]);
        count++;
        //println("min" + count + " " + tempMin);
      }
      else if (trim(_rowNameMaj[i]).length() == 0) { //if next box is empty, add to count
        count++;
        tempMin.add(_rowName[i]);
        //println("min" + count + " ");
      }
      minimums = new String[_bMaj.length][];

     /various unworking attempts below

      //place min values from arraylist into an array
      //String[] temp_bMin = (String[]) tempMin.toArray(new String[0]);
      //fl[] = append((new String(temp_bMin)), fl);

            for (int n = 0; n< bMaj.lenth; n++ ) {
       count[level]mums[n] = (String[]) toArray(new String[0]);
//        println(minimums[n]);
//      }
      tempMin.clear(); //clear temporaryList
    }

  }

  String[] getMaj() {
    return bMaj;
  }

  String[][] getMin() {
    return bMin;
  }
}

任何帮助表示赞赏,非常感谢, Dimitar

4

2 回答 2

0

伪代码:

class Item { 
    String name;
    List<String> values;
}

String[] names = line1.split(",");
String[] data  = line2.split(",");

List<Item> items = new ArrayList<Item>();
Item curItem = null;

for (int i=0; i<data.length, i++) 
{
    if (names[i].isEmpty())
    {
        if (curItem == null) throw "first name is empty";
    }
    else
    {
        curItem = new Item();
        items.add(curItem);
    }
    curItem.values.add(data[i]);
}

这使用一个Item类来保存每个项目和一个List<Item>用于整个事物的类。降级以使用嵌套数组作为练习。

重要如果您需要真正的 CSV 解析(引用字段等),您应该使用 CSV 库进行初始拆分,然后将上述逻辑应用于结果。

于 2012-11-05T20:23:12.537 回答
0

Dimitar... 也许诸如Commons CSV之类的库或该页面上其他库的链接会被证明是有用的?但是,如果您坚持使用自己的 CSV 解析器,请查看java.util.StringTokenizer类。

于 2012-11-05T20:12:50.547 回答