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 ベースの Processing を使用していますが、私の質問は 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

Dimitar... おそらくCommons CSVなどのライブラリや、そのページの他のライブラリへのリンクが役立つでしょうか? ただし、独自の CSV パーサーを開発する必要がある場合は、java.util.StringTokenizerクラスを調べてください。

于 2012-11-05T20:12:50.547 に答える
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 に答える