如何找到只使用,如果还是同时在Java数组复制? [英] How to find duplicates in a java array using only for, if or while?

查看:143
本文介绍了如何找到只使用,如果还是同时在Java数组复制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释我如何找到在java中重复的元素:只使用数组{1,5,7,4,5,1,8,4,1} if或while /而做

can someone explain me how to find duplicate elements in java: array {1,5,7,4,5,1,8,4,1} using only for, if or while/do while?

TNX提前。

大岔

推荐答案

在插入数组中的元素,首先检查数组的内容。如果插入的对象等任何然后不与插入继续进行。

Before you insert an element in the array, check first the content of the array. If the inserting object is equal to any then do not proceed with the insert.

或者,也许试试这个:

int[] arrayObject={1,5,7,4,5,1,8,4,1};
List<Integer> uniqueList=new LinkedList<>();
List<Integer> duplicateList=new LinkedList<>();
for(int i=0; i<arrayObject.length; i++){
    if(!uniqueList.contains(arrayObject[i])){
        uniqueList.add(arrayObject[i]);
    }else if(!duplicateList.contains(arrayObject[i])){
        duplicateList.add(arrayObject[i]);
    }
}
System.out.println("Elements without duplicates: "+uniqueList);
System.out.println("Duplicated elements: "+duplicateList);

输出:

元素没有重复:{1,5,7,4,8}

重复元素:{1,5,4}

Output:
Elements without duplicates: {1, 5, 7, 4, 8}
Duplicated elements: {1, 5, 4}

这篇关于如何找到只使用,如果还是同时在Java数组复制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆