[英]Removing Sublist from ArrayList
為簡單起見,假設我有一個ArrayList
它的索引只包含一個個位數的整數。 例如:
6 4 5 6 0 6 3 4 1 6 1 6 0 6 8 3
我想過濾掉所有出現的子列表6 0 6
,以便新列表變為:
6 4 5 3 4 1 6 1 8 3
有沒有辦法做到這一點? 使用ListIterator
似乎對我不起作用,因為我必須共同考慮三個連續的元素,老實說,我不知道該怎么做。
這是我實現的方法的框架:
public static void filterList(ArrayList<Integer> list) {
ListIterator<Integer> iterator = list.listIterator();
int elem;
while (iterator.hasNext()) {
// Remove any sublist of 6 0 6
}
}
編輯:同樣,為簡單起見,我們假設不會有 60606 或類似的情況。
您可以使用Collections.indexOfSubList
創建高效簡潔的O(nm)解決方案:
public static void removeAllSubList(List<?> list, List<?> subList) {
// find first occurrence of the subList in the list, O(nm)
int i = Collections.indexOfSubList(list, subList);
// if found
if (i != -1) {
// bulk remove, O(m)
list.subList(i, i + subList.size()).clear();
// recurse with the rest of the list
removeAllSubList(list.subList(i, list.size()), subList);
}
}
[編輯 - 更好,單程方法]
自定義,增強的indexOfSublist
從offset
開始搜索; 因此,我們每次刪除時都不會從0
重新啟動(正如我們在使用Collections.indexOfSublist
時所做的那樣,請參閱本答案的底部)。
static <T> int indexOfSublist(List<T> haystack, List<T> needle, int offset){
int toRet=-1;
int needleLen=needle.size();
if(needleLen>0) {
// it makes sense to search
int haystackLen=haystack.size();
for(;offset+needleLen<haystackLen && toRet<0; offset++) {
int compIx;
for(
compIx=0;
(
compIx<needleLen
&& false==haystack.get(offset+compIx).equals(needle.get(compIx))
);
compIx++
);
if(compIx==needleLen) { // found
toRet=offset;
}
}
}
return toRet;
}
public static void filterList(List<Integer> haystack, List<Integer> needle) {
for(
int offset=0, ixOfNeedle=indexOfSublist(haystack, needle, offset);
ixOfNeedle>=0;
ixOfNeedle=indexOfSublist(haystack, needle, offset)
) {
// found one place. We'll continue searching from here next time
offset=ixOfNeedle;
//////////////////////////////////////////
// for a better removal sequence, see the
// 4castle's answer using sublists
for(int i=needle.size(); i>0; i--) {
haystack.remove(ixOfNeedle);
}
}
}
Collections.indexOfSublist就是你追求的目標。
public static void filterList(ArrayList<Integer> haystack, List<Integer> needle) {
for(
int ixOfNeedle=Collections.indexOfSublist(haystack, needle);
ixOfNeedle>=0;
ixOfNeedle=Collections.indexOfSublist(haystack, needle)
) {
for(int i=needle.size(); i>0; i--) {
haystack.remove(ixOfNeedle);
}
}
}
我建議您在將ArrayList
放入ListIterator
之前搜索它。
public static void filterList(ArrayList<Integer> list) {
bool firstInstance = false; //Saying we having found our first instance of our sub list
for(int i=0;i<list.size();++i) {
if(list.get(i) == 6) //Checks to see if our first index is a 6 or it pointless to check the next two numbers i.e. wasting resources
if(list.get(i+1) == 0 && list.get(i+2) == 6 && !firstInstance) { //Make sure it a 6 0 6 list
list.remove(i); //Removes first one
list.remove(i); //Removes second one which now became our current index number
list.remove(i); //Removes third one which now became our current index number
} else
firstInstance = true; //Our first instances has been found and will now remove duplicate ones!
}
ListIterator<Integer> iterator = list.listIterator();
int elem;
while (iterator.hasNext()) {
// Remove any sublist of 6 0 6-- Already Done
}
}
您可以使用數組和列表組合作為查找以下解決方案,希望它可以幫助您。
public void testData()
{
int tempArray[] = {6, 4, 5, 6, 0, 6, 3, 4, 1, 6, 1, 6, 0, 6, 8, 3};
List<Integer> resultArray = new ArrayList<>();
for(int i = 0; i < tempArray.length; i++)
{
if(tempArray[i] == 6 && tempArray[i+1] == 0 && tempArray[i + 2] == 6)
{
i += 2;
}
else
{
resultArray.add(tempArray[i]);
}
}
for(int tempInt : resultArray)
{
System.out.print("\t" + tempInt);
}
}
注意:您可以根據上述功能的要求從側面傳遞數組或返回結果。
//if
List<String> originalList = new ArrayList<>();
originalList.add("A");
originalList.add("B");
originalList.add("C");
originalList.add("D");
//and
List<String> subList = new ArrayList<>();
subList.add("A");
subList.add("C");
//then
originalList = originalList.stream().filter(x -> !subList.contains(x)).collect(Collectors.toList());
//originalList should now contain {"B","D"}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.