繁体   English   中英

从 ArrayList 中删除子列表

[英]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);
    }
}

Ideone演示

[编辑 - 更好,单程方法]

自定义,增强的indexOfSublistoffset开始搜索; 因此,我们每次删除时都不会从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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM