【问题标题】:i want to remove the duplicate sub integer arraylist from an integer arraylist in java我想从java中的整数arraylist中删除重复的子整数arraylist
【发布时间】:2020-05-22 07:05:03
【问题描述】:

给你一个嵌套数组列表,例如: 列表 = [[-1,2,1],[-2,-2,4],[-1,2,-1],[-1,-2,3],[-1,2,-1 ]]

我想要这样的输出: [[-1,2,1],[-2,-2,4],[-1,-2,3]]

但它不太可能与我正在使用的代码一起提供......

for(int i=0;i<list.size();i++){
for(int j=i+1;j<list.size();j++){
if(list.get(i).eqauls(list.get(j)))
 {
 list.remove(list.get(j));
  }
  }
  }
 System.out.println(list);

我已经这样做了,但它没有采取,重复仍然存在 所以我用另一种方式做了这样的事情......

List<List<Integer>> list2=  new ArrayList<List<Integer>>();
for(int i=0;i<list.size();i++){
for(int j=i+1;j<list.size();j++){
if(!list.get(i).eqauls(list.get(j)))
 {
 List<Integer> p= new ArrayList<Integer>();
 for(int m=0;m<list.size();m++){
 for(int n=0;n<list.get(i).size();n++){
 p.add(list.get(i).get(m));
 list2.add(p);
 }
 }
 }
System.out.println(list2);

输出:运行时错误 在这种情况下我应该怎么做......只使用数组列表数据结构......

【问题讨论】:

  • 你考虑[-1,2,1][-1,2,-1]。需要明确的是,这两个在什么基础上被认为是相同的?
  • 你的 j 上限好像有误
  • 您在问题中写道:输出:运行时错误 也许您可以edit 您的问题并发布您看到的实际错误消息以及堆栈跟踪?我猜这是因为你的这行代码:List&lt;List&lt;Integer&gt;&gt; list2= new ArrayList&lt;List&lt;Integer&gt;&gt;(); 我看不到你在哪里填充list2。您正在创建一个空列表。

标签: java arraylist


【解决方案1】:

策略

我们可以构建一个树形数据结构,其路径正是您的主列表中包含的唯一子列表。构建树后,我们可以对其进行迭代,例如使用递归算法,并重新构建列表的主列表,没有任何重复。

代码

import java.util.*;

public class ListUnduper{

     public static void main(String []args){
        Tree root = new Tree();
        List<List<Integer>> list = new ArrayList<>();
        List<Integer> first = new ArrayList<Integer>(); 
        first.add(1); first.add(2);
        List<Integer> second = new ArrayList<Integer>(); 
        //Add more lists here if you like
        list.add(first); list.add(second);
        for(int i=0;i<list.size();i++){
          List<Integer> inner = list.get(i);
          Tree current = root;
          for(int j=i+1;j<inner.size();j++){
            int nextElement = inner.get(j);        
            if(!current.children.containsKey(nextElement)){
              current.children.put(nextElement, new Tree());
            }
            current = current.children.get(nextElement);
           }
        }

        List<List<Integer>> master = new ArrayList<List<Integer>>();
        List<Integer> emptyPrefix = new ArrayList<Integer>();
        addAllToList(emptyPrefix, master, root);    

        //master now should contain all non-dupes 
     }

    static void addAllToList(List<Integer> prefix, List<List<Integer>> master, Tree tree){
      for(Map.Entry<Integer,Tree> entry : tree.children.entrySet()){
        Tree nextTree = entry.getValue();  
        //I believe this makes a deep copy
        List<Integer> nextPrefix = new ArrayList<>(prefix);
        nextPrefix.add(entry.getKey());
        if(nextTree.children.isEmpty()){
          master.add(nextPrefix);
        }
        else{
          addAllToList(nextPrefix, master, tree);
        }
      }
    }
}

class Tree{
  HashMap<Integer, Tree> children = new HashMap<Integer, Tree>();
}

警告:如果您的列表很大,使用递归可能会导致 Stackoverflow 错误。在这种情况下,建议改用 while 循环,但在这种情况下,算法的编码可能会更复杂。

元素顺序说明

正如this alternative answer 指出的那样,主列表中列表的原始顺序可能很重要。上述解决方案不保证保留这样的顺序。

【讨论】:

  • 您的代码包含几个问题。例如,您正在使用原始类型,并且您正在使用原始类型作为类型参数(无法编译)。
  • @MCEmperor V. 是的。用工作代码替换惰性算法草图
【解决方案2】:

我猜这可能是一个学校作业,旨在让您练习使用列表和迭代器以及equals() 方法,甚至可能是ComparatorComparable。而且我很确定您还没有了解stream API,但是由于您的代码使用List,这意味着您已经了解了collections framework,所以我不需要向您解释@987654325是什么@ 是。在任何情况下,您都可以使用 stream APIcollections 框架 轻松完成您的任务,如下面的代码所示。 (请注意,以下代码使用 Java 9 中引入的方法。)

/* Required imports
 * 
 * import java.util.List;
 * import java.util.Set;
 * import java.util.stream.Collectors;
 */
List<List<Integer>> list2 = List.of(List.of(-1,2,1),
                                    List.of(-2,-2,4),
                                    List.of(-1,2,-1),
                                    List.of(-1,-2,3),
                                    List.of(-1,2,-1));
Set<List<Integer>> noDups = list2.stream()
                                 .collect(Collectors.toSet());
System.out.println(noDups);

运行上述代码会产生以下输出。
(请注意,迭代 Set 不保证任何特殊排序。)

[[-2, -2, 4], [-1, -2, 3], [-1, 2, -1], [-1, 2, 1]]

参考接口java.util.List的方法equals()

编辑

由于Colm Bhandal的评论,并从artmmslvanswer获得灵感,如果顺序很重要,那么下面的代码,对上面的代码稍作修改,会维持秩序。

List<List<Integer>> list2 = List.of(List.of(-1,2,1),
                                    List.of(-2,-2,4),
                                    List.of(-1,2,-1),
                                    List.of(-1,-2,3),
                                    List.of(-1,2,-1));
Set<List<Integer>> noDups = list2.stream()
                                 .collect(LinkedHashSet::new,
                                          LinkedHashSet::add,
                                          LinkedHashSet::addAll);
System.out.println(noDups);

这段代码的输出是:

[[-1, 2, 1], [-2, -2, 4], [-1, 2, -1], [-1, -2, 3]]

【讨论】:

    【解决方案3】:

    把你的子数组放到LinkedHashSet

    这个集合按相加的顺序存储对象(不存储相等的对象)

    【讨论】:

    • 简洁,并回答了这个问题,但我认为您应该添加一些代码来演示如何完成您提出的解决方案。
    【解决方案4】:

    您需要自己编写代码,没有什么神奇的功能可以为您完成。

    你可以看到下面的示例代码。

    const input = [[-1,2,1],[-2,-2,4],[-1,2,-1],[-1,-2,3],[-1,2,-1]]
    
    const removeDuplicate = list => {
      const set = new Set()
      for (const item of list) {
         set.add(item.join('|'))
      }
      const result = Array.from(set).map(strItem => {
        const resultItem = strItem.split('|').map(item => parseInt(item))
        return resultItem
      })
      return result
    }
    
    const result = removeDuplicate(input)
    
    console.log(result)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-14
      • 2018-05-29
      • 2020-03-06
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2012-08-25
      相关资源
      最近更新 更多