[英]Split a list of values based on a value into a list using Java
考慮一個長值列表[1,2,3,4,2,3,6,3,7,3]
。 我需要執行 java 程序來根據值拆分列表。 假設value = 3
。 每當我發現value = 3
。 我需要以這種方式拆分列表[[1,2],[3],[4,2],[3],[6],[3],[7],[3]]
。
我編寫了以下代碼來實現問題陳述:
public class Solution {
public static void main(String args[]) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(2);
list.add(3);
list.add(6);
list.add(3);
list.add(7);
System.out.println(list);
int val = 3;
List<List<Integer>> finalList = new ArrayList<>();
List<Integer> tempList = new ArrayList<>();
for (int i : list) {
if (i != val) {
tempList.add(i);
} else {
finalList.add(new ArrayList<>(tempList));
finalList.add(Arrays.asList(i));
tempList.clear();
}
}
System.out.println(finalList);
}
}
Output: [[1, 2], [3], [4, 2], [3], [6], [3]]
預期 Output: [[1,2],[3],[4,2],[3],[6],[3],[7]]
最后一個數字 [7] 未添加到 finalList。 任何建議!
問題是您在數組中添加了整個起始列表,
相反,您需要記住其他數組,以便在遇到 3 時可以推動這個數組。
List<Integer> list_i_want_to_push = new ArrayList<>();
for (int i =0; i < list.size(); i++){
list_i_want_to_push.add(list.get(i));
if(val == list.get(i)){
list_i_want_to_push.remove(i);
finalList.add(list_i_want_to_push);
finalList.add(Arrays.asList(val));
list_i_want_to_push = []; //empty your array
}
}
System.out.println(finalList);
我相信這可以滿足您的要求,但我必須做出一些假設。
import java.util.ArrayList;
import java.util.List;
public class SplitListOnValue {
public static void main(String args[]) {
List<Integer> list = new ArrayList<>(List.of(3,3,2,3,4,2,3,3,3,6,6,6,3,7,3,3));
System.out.println(list);
int splitVal = 3;
List<Integer> temp = new ArrayList<>();
List<List<Integer>> finalList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
int item = list.get(i);
if (item == splitVal) {
// add both list and singleton list if temp not empty
// other wise just add singletone list
if (!temp.isEmpty()) {
finalList.add(new ArrayList<>(temp));
}
finalList.add(new ArrayList<>(List.of(splitVal)));
// clear temp list and restart process
temp.clear();
continue;
}
// still accumulating
temp.add(item);
}
System.out.println(finalList);
}
}
印刷
[[3], [3], [2], [3], [4, 2], [3], [3], [3], [6, 6, 6], [3], [7], [3], [3]]
試試這樣:
public static void main(String[] args) {
// The number you want to split by
int delimiter = 3;
List<Integer> input = Arrays.asList(5, 3, 10, 4, 3, 5, 6, 3, 4);
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>()); // Add a list so that we have something to work with
for (int i = 0; i < input.size(); i++) {
// Get the next value in the list
int val = input.get(i);
// If the current value equals the one we want to split by...
if (val == delimiter) {
//... add a new list containing only the delimiter...
result.add(Arrays.asList(delimiter));
//... and add a new list so the other values will be added correctly
result.add(new ArrayList<>());
} else // if this is just a random number, add it to the last list
result.get(result.size() - 1).add(val);
}
// If the last number in your input is equal to the delimiter, there will be an empty
// list at the end, which can be prevented this way
if (result.get(result.size() - 1).size() == 0)
result.remove(result.size() - 1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.