[英]How to delete sublists from a list in java with specific substring as a start index and specific substring as an end index?
Every sublist starts with code 0011 and ends with code 0082. If the end code 0082 has value "W", that sublist should be deleted.每个子列表都以代码 0011 开始并以代码 0082 结束。如果结束代码 0082 的值为“W”,则应删除该子列表。 Can someone please help, what approach to be taken to delete all the sublists from the list, which are not containing desired value of the end code?有人可以帮忙,采取什么方法从列表中删除所有不包含最终代码所需值的子列表?
0011 : 4202142599 0011 : 4202142599
0012: 031121 0012:031121
0009: CONG TY TNHH MTV THUONG MAI DICH VU AN NHIEN 0009: CONG TY TNHH MTV THUONG MAI DICH VU AN NHIEN
0006: THON HOACH THON XA DINH TANG HUYEN YEN DINH TINH THANH HOA 0006: THON HOACH THON XA DINH TANG HUYEN YEN DINH TINH THANH HOA
1011: VN 1011:越南
0330: 00133 0330:00133
0269: annhien Farm 0269:安宁农场
1021: *AN/NHIEN/FARM& 1021:*AN/NHIEN/农场&
1021: *AN/NHIEN& 1021:*安/日&
0045: 31 0045:31
0082 : M 0082 :中号
0011 : 4202142599 0011 : 4202142599
0012: 031121 0012:031121
0009: CONG TY TNHH MTV THUONG MAI DICH VU AN NHIEN 0009: CONG TY TNHH MTV THUONG MAI DICH VU AN NHIEN
0006: THON HOACH THON XA DINH TANG HUYEN YEN DINH TINH THANH HOA 0006: THON HOACH THON XA DINH TANG HUYEN YEN DINH TINH THANH HOA
1011: VN 1011:越南
0330: 00133 0330:00133
0269: annhien Farm 0269:安宁农场
1021: *AN/NHIEN/FARM& 1021:*AN/NHIEN/农场&
1021: *AN/NHIEN& 1021:*安/日&
0045: 31 0045:31
0082 : W 0082 :W
0011 : 4202142782 0011 : 4202142782
0012: 041121 0012:041121
0009: G UNIT BRANDS INC. 0009: G 单位品牌公司
0006: TH FLOOR HUDSON PLACE HOBOKEN NJ 0006: TH FLOOR HUDSON PLACE HOBOKEN NJ
1011: US 1011:美国
0330: 00134 0330:00134
0269: GREEN LIGHT GANG 0269:绿灯帮
1021: *GREEN/LIGHT/GANG& 1021:*绿色/灯光/帮派&
1021: *GREEN/LIGHT& 1021:*绿色/灯光&
0045: 25 0045:25
0082 : W 0082 :W
Start by creating a List to hold the final result:首先创建一个 List 来保存最终结果:
List<List<String>> finalList = new ArrayList<>();
Iterate through the current Main Data List of sub-lists but first create another List<String> listItems = new ArrayList<>();
遍历子列表的当前主数据列表,但首先创建另一个List<String> listItems = new ArrayList<>();
to hold the item from Main List currently being processed.保存当前正在处理的主列表中的项目。 If the end of a sub-list is encountered that does not end with "W" then add it to the listItems list and then add the listItems list to the finalList.如果遇到不以“W”结尾的子列表的结尾,则将其添加到 listItems 列表,然后将 listItems 列表添加到 finalList。 Essentially, we ignore sub-lists that end with "0082: W".本质上,我们忽略了以“0082:W”结尾的子列表。 Below is an example of this process (read all the comments in code):下面是这个过程的一个例子(阅读代码中的所有注释):
// Holds the main List read in from file.
List<String> mainList = new ArrayList<>();
/* Will hold the List result after processing.
This result will overwrite the list above
when all is done. */
List<List<String>> finalList = new ArrayList<>();
// The data file that contains List Items.
String dataFilePath = "MainList.txt";
// Read in List data from file...
try (Scanner reader = new Scanner(new File(dataFilePath))) {
while (reader.hasNextLine()) {
mainList.add(reader.nextLine());
}
}
catch (FileNotFoundException ex) {
// Do what you like with Exception...
System.err.println(ex.getMessage());
}
/* Iterate through the Main List. Ignore Sub-Lists
which contains "0082 : W" */
List<String> listItems = new ArrayList<>(); // Create a List for currently processed sub-list items
for (String lstStrg : mainList) {
/* If line starts with "0082" to indicate the end of a sub-list
but does NOT end with a "W" then... */
if (lstStrg.startsWith("0082") && !lstStrg.endsWith("W")) {
listItems.add(lstStrg); // Add item to current list
finalList.add(new ArrayList<>(listItems)); // Add the current list to final List as a new list object
listItems.clear(); // Clear the current active list in prep for a new sub-list.
}
/* If line starts with "0082" to indicate the end of a sub-list
but DOES end with a "W" then Clear the current active list in
prep for a new sub-list. */
else if (lstStrg.startsWith("0082") && lstStrg.endsWith("W")) {
listItems.clear();
}
/* Otherwise just add Main List sub-list item to the
current active list. */
else {
listItems.add(lstStrg);
}
}
/* Clear the Main List then copy the Final Result List into
the Original Main List. This will then be the same as
though all Sub-Lists that ended with "0082 : W" were deleted. */
mainList.clear();
for (List<String> lst : finalList) {
mainList.addAll(lst);
}
// Display the Main List with the results of this operation.
for (String strg : mainList) {
System.out.println(strg);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.