我想从java中的整数arraylist中删除重复的子整数arraylist
将为您提供一个嵌套数组列表,例如: 列表=[-1,2,1],-2,-2,4],-1,2,-1],-1,-2,3],-1,2,-1]] 我想要这样的输出: [1,2,1],-2,-2,4],-1,-2,3]] 但它不太可能与我正在使用的代码一起出现我想从java中的整数arraylist中删除重复的子整数arraylist,java,arraylist,Java,Arraylist,将为您提供一个嵌套数组列表,例如: 列表=[-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(li
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);
for(int i=0;i将子数组置于
此集合按添加顺序存储对象(不存储相等的对象)策略
我们可以构建一个树数据结构,其路径正好是主列表中包含的唯一子列表。一旦构建了树,我们可以对其进行迭代(例如,使用递归算法),并重新构建列表主列表,而不存在任何重复项
代码
import java.util.*;
公共类列表管理器{
公共静态void main(字符串[]args){
树根=新树();
列表=新的ArrayList();
List first=new ArrayList();
第一.加入(1);第一.加入(2);
第二个列表=新的ArrayList();
//如果愿意,请在此处添加更多列表
list.add(第一);list.add(第二);
对于(inti=0;i我猜这可能是一个学校作业,旨在让你练习使用列表和迭代器以及equals()
方法,甚至可能是。我很确定您还没有了解,但是由于您的代码使用了列表,这意味着您已经了解了,所以我不需要向您解释什么是。在任何情况下,您的任务都可以很容易地使用流API和集合框架来完成,如bel中所示ow代码。(注意,下面的代码使用Java9中引入的方法。)
/*所需的导入
*
*导入java.util.List;
*导入java.util.Set;
*导入java.util.stream.collector;
*/
List list2=列表的列表(列表的(-1,2,1),
(-2,-2,4)的列表,
清单-1,2,-1),
清单-1,-2,3),
(-1,2,-1))的列表;
Set noDups=list2.stream()
.collect(收集器.toSet());
系统输出打印LN(节点);
运行上述代码将产生以下输出。
(请注意,迭代集合
并不保证任何特殊顺序。)
请参阅接口方法java.util.List
编辑
根据artmmslv的评论,并从artmmslv中得到启发,如果订单很重要,那么下面的代码(从上面的代码稍微修改)将维持订单
List list2=List.of(List.of(-1,2,1),
(-2,-2,4)的列表,
清单-1,2,-1),
清单-1,-2,3),
(-1,2,-1))的列表;
Set noDups=list2.stream()
.collect(LinkedHashSet::新建,
LinkedHashSet::添加,
LinkedHashSet::addAll);
系统输出打印LN(节点);
此代码的输出为:
[[-1, 2, 1], [-2, -2, 4], [-1, 2, -1], [-1, -2, 3]]
你需要自己编写代码,没有神奇的功能可以为你做
您可以看到下面的示例代码
const input=[-1,2,1],-2,-2,4],-1,2,-1],-1,-2,3],-1,2,-1]]
const removeDuplicate=列表=>{
常量集=新集()
用于(列表中的常量项){
set.add(item.join(“|”))
}
const result=Array.from(set.map)(strItem=>{
const resultItem=strItem.split(“|”).map(item=>parseInt(item))
返回结果
})
返回结果
}
const result=removeDuplicate(输入)
控制台。日志(结果)< /> >你考虑<代码> [-1,21] < /代码>和<代码> [-1,2,-1 ]
。需要明确的是,这两个变量在什么基础上被认为是相同的?您的j上限似乎是错误的您在问题中写道:输出:运行时错误也许您可以将您的问题和看到的实际错误消息与堆栈跟踪一起发布?我猜这是因为您的代码行:列表2=new ArrayList();
我看不出您在哪里填充列表2
。您正在创建一个空列表。您的代码包含几个问题。例如,您使用原始类型,并且您使用一个基本类型作为类型参数(不编译)@mcepender V.true。懒惰的算法草图替换为工作代码简洁,并回答了问题,但我认为您应该添加一些代码来演示如何完成建议的解决方案。
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>();
}
[[-2, -2, 4], [-1, -2, 3], [-1, 2, -1], [-1, 2, 1]]
[[-1, 2, 1], [-2, -2, 4], [-1, 2, -1], [-1, -2, 3]]