我有一个如下列表。
["A $0; B $0; C $0; D $0",
"B $5.8M; A $24.8M",
"A $9k; B $20k",
"A TBD; B TBD",
"C $36k; est. A $36k",
"A TBD; B TBD",
"D TBD; B TBD",
"D $1.1m; B $3m",
"A $3.86m; D $7.08m",
"C TBD; B TBD"]
预期输出:
A = [$0, $24.8M, $9k, TBD, $36k, TBD, $0, $0, $3.86m, 0]
B = [$0, $5.8M,$20k, TBD, $0, TBD, TBD, $3m, $0, TBD ]
C = [$0, $0, $0, $0, $36k, $0, $0, $0, $0, TBD]
D = [$0, $0, $0, $0, $0, $0, TBD, $1.1m, $7.08m, $0 ]
我希望将它分成4 List<String>
,即A包含A的所有值,如果A不存在于源中,则为0.同样分为B,C和D.是否有一种简单的方法实现这个目标?
答案 0 :(得分:0)
你的问题确实在空中留下了相当多的实际需要,但正如我通过阅读评论所理解的那样......
您的列表中有4个项目(A,B,C和D)。您想要创建与每个特定项目相关的列表。如果主数据列表中有10个数据元素,那么每个项目列表(A到D)最终也应该有10个值元素。这是因为如果某个特定项目未包含在主要数据元素中(例如“A”或“B”或“C”或“D”),则会将“$ 0”的值添加到该特定项目列表中无论如何。这将始终确保每个列表始终包含相同数量的元素。
真正的快手我会说为每个项目创建一个布尔标志将是最简单的。最初在主数据列表迭代中的所有四个标志设置为false。在解析(拆分)每个主数据元素并将特定的项值添加到其特定列表时,将其标志设置为true。在迭代块结束时检查哪些标志仍为false。如果有,则将“$ 0”添加到其相关列表中。在下一个主数据列表迭代时,整个过程再次完成并继续,直到所有主数据列表元素都已处理完毕。
它可能看起来像这样:
// Main Data List
List<String> mainDataList = new ArrayList<>();
mainDataList.add("A $0; B $0; C $0; D $0");
mainDataList.add("B $5.8M; A $24.8M");
mainDataList.add("A $9k; B $20k");
mainDataList.add("A TBD; B TBD");
mainDataList.add("C $36k; est. A $36k");
mainDataList.add("A TBD; B TBD");
mainDataList.add("D TBD; B TBD");
mainDataList.add("D $1.1m; B $3m");
mainDataList.add("A $3.86m; D $7.08m");
mainDataList.add("C TBD; B TBD");
// Declare Lists for each Item (A to D) Value
List<String> itemListA = new ArrayList<>();
List<String> itemListB = new ArrayList<>();
List<String> itemListC = new ArrayList<>();
List<String> itemListD = new ArrayList<>();
boolean aHit, bHit, cHit, dHit; // Flags
for (int i = 0; i < mainDataList.size(); i++) {
aHit = false; bHit = false;
cHit = false; dHit = false;
// RegEx "\\s+" is used below. It means one or more spaces.
// Split the current Main Data List element string
String[] tmpString = mainDataList.get(i).trim().split(";\\s+");
// Iterate through the created array from the Main Data List element split.
for (int j = 0; j < tmpString.length; j++) {
// Split each Item element to Item and Value
String[] data = tmpString[j].replaceAll("est.\\s+", "").split("\\s+");
// Is it Item A
if (data[0].equalsIgnoreCase("a")) {
itemListA.add(data[1]);
aHit = true;
}
// Is it Item B
else if (data[0].equalsIgnoreCase("b")) {
itemListB.add(data[1]);
bHit = true;
}
// Is it Item C
else if (data[0].equalsIgnoreCase("c")) {
itemListC.add(data[1]);
cHit = true;
}
// Is it Item D
else if (data[0].equalsIgnoreCase("d")) {
itemListD.add(data[1]);
dHit = true;
}
}
// Were all items hit in this current iteration
// of the Main Data List.
if (!aHit) { itemListA.add("$0"); } // If no A in line add 0 to A list
if (!bHit) { itemListB.add("$0"); } // If no B in line add 0 to B list
if (!cHit) { itemListC.add("$0"); } // If no C in line add 0 to C list
if (!dHit) { itemListD.add("$0"); } // If no D in line add 0 to D list
}
// Display the Lists in Console....
// Since all Lists will be the same size we
// can use one loop.
System.out.println("\nList A\t\tList B\t\tList C\t\tList D");
System.out.println("======\t\t======\t\t======\t\t======");
for (int i = 0; i < itemListA.size(); i++) {
System.out.print(itemListA.get(i)+"\t\t" + itemListB.get(i)+"\t\t" +
itemListC.get(i)+"\t\t" + itemListD.get(i));
System.out.println("");
}
输出应如下所示:
List A List B List C List D
====== ====== ====== ======
$0 $0 $0 $0
$24.8M $5.8M $0 $0
$9k $20k $0 $0
TBD TBD $0 $0
$36k $0 $36k $0
TBD TBD $0 $0
$0 TBD $0 TBD
$0 $3m $0 $1.1m
$3.86m $0 $0 $7.08m
$0 TBD TBD $0