java字符串数组转换成二维数组
aa$Node5,
bd$Node5,
dc$Node21,
ht$Node20,
gf$Node20,
cc$Node20,
ui$Node4
转换成二维数组,根据 $ 符号后面的Node节点做依据。
效果:
list[0][0] = "aa";
list[0][1] = "bd";
list[1][0] = "dc";
list[2][0] = "ht";
list[2][1] = "gf";
list[2][2] = "cc";
[解决办法]
String text = "aa$Node5,\nbd$Node5,\ndc$Node21,\nht$Node20,\ngf$Node20,\ncc$Node20,\nui$Node4";
String[] rows = text.split(",\n");
List<Object> result = new ArrayList<Object>();
String node = null;
List<String> values = new ArrayList<String>();;
for (String row: rows){
String[] cols = row.split("[$]");
if(!cols[1].equals(node) && node!=null){
result.add(values.toArray());
values = new ArrayList<String>();
}
values.add(cols[0]);
node = cols[1];
}
result.add(values.toArray());
for(Object row: result){
System.out.println(Arrays.toString((Object[]) row));
}