我已經在這個問題上摸到了一段時間了。我基本上試圖從一組CSV數據中生成一個樹層次結構。 CSV數據不一定是有序的。這就像下面這樣:從csv生成樹結構
Header: Record1,Record2,Value1,Value2
Row: A,XX,22,33
Row: A,XX,777,888
Row: A,YY,33,11
Row: B,XX,12,0
Row: A,YY,13,23
Row: B,YY,44,98
我試圖讓分組的執行方式儘可能靈活。對於分組的最簡單的將這樣做對記錄1和RECORD2與RECORD2下存儲的值1和值2,使我們得到如下輸出:
Record1
Record2
Value1 Value2
具體做法是:
A
XX
22,33
777,888
YY
33,11
13,23
B
XX
12,0
YY
44,98
我存儲我目前在List中的組設置 - 我不知道這是否妨礙了我的想法。此列表中包含組例如層次:
Record1 (SchemaGroup)
.column = Record1
.columns = null
.childGroups =
Record2 (SchemaGroup)
.column = Record1
.columns = Value1 (CSVColumnInformation), Value2 (CSVColumnInformation)
.childGroups = null
該代碼,這看起來像如下:
private class SchemaGroup {
private SchemaGroupType type = SchemaGroupType.StaticText; // default to text
private String text;
private CSVColumnInformation column = null;
private List<SchemaGroup> childGroups = new ArrayList<SchemaGroup>();
private List<CSVColumnInformation> columns = new ArrayList<CSVColumnInformation>();
}
private enum SchemaGroupType {
/** Allow fixed text groups to be added */
StaticText,
/** Related to a column with common value */
ColumnGroup
}
我都有點吃力生產這種算法,冥思苦想的底層結構使用。目前我使用自己的包裝類從上到下解析CSV:
CSVParser csv = new CSVParser(content);
String[] line;
while((line = csv.readLine()) != null) {
...
}
我只是試圖啓動我的編碼大腦。
有什麼想法?
什麼是大局?如果行A,XX,12,34,你怎麼知道12和34屬於A或XX? – palacsint