java删除数组中重复元素

来源:互联网 发布:js 增加option 编辑:程序博客网 时间:2024/06/06 10:02
<iframe id="BAIDU_DUP_fp_iframe" src="https://pos.baidu.com/wh/o.htm?ltr="></iframe>

Java SE

GXW33GXW3310-08 14:30
等级 T1 28次回复

java删除数组中重复元素

java删除数组中重复元素

<iframe id="iframeu2634446_0" src="http://pos.baidu.com/hcrm?sz=1366x60&amp;rdid=2634446&amp;dc=3&amp;di=u2634446&amp;dri=0&amp;dis=0&amp;dai=1&amp;ps=338x0&amp;coa=at%3D3%26pat%3D15%26tn%3Dtemplate_inlay_all_mobile_lu_native%26rss1%3D%2523FFFFFF%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26ptFS%3D16%26ptFC%3D%2523000000%26ptFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26ptFW%3D1%26conpl%3D0%26conpr%3D1%26conpt%3D0%26conpb%3D0%26rsi1%3D60%26ptn%3D1%26ptp%3D0%26itecpl%3D10%26piw%3D0%26pih%3D0%26ptDesc%3D0%26ptLogo%3D0&amp;dcb=___adblockplus&amp;dtm=HTML_POST&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1492612855723&amp;ti=CSDN%20%E8%AE%BA%E5%9D%9B&amp;ari=2&amp;dbv=2&amp;drs=3&amp;pcs=1349x611&amp;pss=1349x13497&amp;cfv=0&amp;cpl=27&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1492612855&amp;rw=611&amp;ltu=http%3A%2F%2Fbbs.csdn.net%2Fwap%2Ftopics%2F370233547&amp;ecd=1&amp;uc=1366x728&amp;pis=-1x-1&amp;sr=1366x768&amp;tcn=1492612856&amp;qn=f762961fcc8e15c6&amp;tt=1492612855696.33.136.140" width="1366" height="60" align="center,center" vspace="0" hspace="0" scrolling="no"></iframe>
soli11722984soli1172298410-08 14:30
等级 T1 1楼

倒到SET裏面去

0 0
woaini314woaini31410-08 14:35
等级 T1 2楼

element as map key

0 0
chruanchruan10-08 14:37
等级 T1 3楼

可以排序,再删除

0 0
ForeverLonely00ForeverLonely0010-08 15:34
等级 T1 4楼

int[] arr = new int[]{1,2,3,4,23,3,5,1};
ArrayList<Integer> arrList = new ArrayList<Integer>();
for(int i=0; i<arr.length; i++)
{
if(!arrList.contains(arr[i]))
arrList.add(arr[i]);
}
Iterator<Integer> inter = arrList.iterator();
while(inter.hasNext())
System.out.println(inter.next());

0 0
jiangxiayangjiangxiayang10-08 15:40
等级 T1 5楼

package demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Shanchu {
public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
List<Integer> numList = new ArrayList<Integer>();
for (int i : nums)
numList.add(i);
System.out.println(numList);

//做删除
Iterator<Integer> it = numList.iterator();
int temp = -1;
if (it.hasNext())
temp = it.next();
while (it.hasNext()) {
int i = it.next();
if (i == temp) {
it.remove();
} else {
temp = i;
}
}
System.out.println(numList);
}

}

0 0
soli11722984soli1172298410-08 15:52
等级 T1 6楼

都這麽複雜啊


public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
List<Integer> numList = new ArrayList<Integer>();
for (int i : nums)
numList.add(i);
Set<Integer> numSet = new HashSet<Integer>();
numSet.addAll(numList);
System.out.println(numSet);
}

2 0
feifeikubfeifeikub10-08 15:53
等级 T1 7楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

顶一个

0 0
changtianshuiyuechangtianshuiyue10-08 16:29
等级 T1 8楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

牛人,膜拜下,怎么想到的

0 0
lliiqianglliiqiang10-08 17:22
等级 T1 9楼

从第一个遍历,查看前面是否有重复的,有就删除

0 0
dy110936dy11093610-08 17:30
等级 T1 10楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊

Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nums)
 ……

先顶一下,但是这样顺序不就乱了么。

0 0
jiunizhuaijiunizhuai10-08 17:46
等级 T1 11楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊

Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nums)
 ……



这个不错,通过集合转换,用treeset 还可以排序,呵呵

0 0
TKD03072010TKD0307201010-08 17:47
等级 T1 12楼

用set集合就行了

0 0
taodengwentaodengwen10-08 18:04
等级 T1 13楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nu……

zzz~~

0 0
walkman_22walkman_2210-09 09:22
等级 T1 14楼

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code

    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : nu……


我也是这个思路。

0 0
cbxjjcbxjj10-09 09:29
等级 T1 15楼

不用set也很容易的

public static void main(String[] args) {
String[] s = {"1","10","15","14","111","133","12","13","1","13"};
List<String> l = new ArrayList<String>();
for(String a:s){
if(!l.contains(a)){
l.add(a);
}
}
System.out.println(l);
}

0 0
liyangyun1986liyangyun198610-09 09:32
等级 T1 16楼

最简单的方法就是丢到set集合中去~

0 0
gl74gs48gl74gs4810-09 10:17
等级 T1 17楼


import java.util.*;
class TestSet 
{
public static void main(String[] args) 
{
Integer[] nums = { 5, 5, 6, 6, 6, 8, 8, 7, 11, 12, 12 };
HashSet hset = new HashSet(Arrays.asList(nums));
Iterator i = hset.iterator();
while(i.hasNext()){
System.out.println(i.next());
}            

}
}

0 0
ksqqxqksqqxq10-09 10:33
等级 T1 18楼

牛人太多了,膜拜中。。。。。。

0 0
chcchbchcchb10-09 11:15
等级 T1 19楼

set方法不错

0 0
snowday88snowday8810-09 12:25
等级 T1 20楼

Set有一个构造方法 参数就是list

引用 6 楼 soli11722984 的回复:
都這麽複雜啊


Java code


    public static void main(String[] args) {
        int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        List<Integer> numList = new ArrayList<Integer>();
        for (int i : ……

0 0
dfox_javadfox_java10-09 17:18
等级 T1 21楼


//简单的for循环
for(int i=1;i<arr.length;i++){
  for(j=0;j<i;j++){
     if(arr[i]==arr[j]){
       随便干嘛。。
        

}
}
}

0 0
wingson_shenwingson_shen10-09 17:39
等级 T1 22楼

我们项目中的工具类,用来去掉List中空值和相同项的。


public <T> List<T> removeSameItem(List<T> list) {
List<T> difList = new ArrayList<T>();
for(T t : list){
if(t != null && !difList.contains(t)){
difList.add(t);
}
}
return difList;
}


0 0
leisoreleisore10-09 18:45
等级 T1 23楼

引用 5 楼 jiangxiayang 的回复:
package demo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Shanchu {
public static void main(String[] args) {
int[] nums = { 5, 6, 6, 6, 8, 8, ……

再稍微简单点的(不考虑性能的话):

    Integer[] nums = { 5, 6, 6, 6, 8, 8, 7 };
    Set<Integer> numSet = new LinkedHashSet<Integer>(Arrays.asList(nums));
    System.out.println(numSet);

0 0
afgasdgafgasdg10-09 22:47
等级 T1 24楼

一句话搞定:
System.out.println(new LinkedHashSet<Integer>(Arrays.asList(5, 6, 6, 6, 8, 8, 7)));

0 0
WWdsaf5977129WWdsaf597712910-09 22:59
等级 T1 25楼

牛人多,继续拜读。。。。。。。。。

0 0
fuwenhaifuwenhai10-10 09:33
等级 T1 26楼

先把数组中元素放入set集合中啊,然后从set中取出来,得到的就是去除重复数据之后的

0 0
jesus7_weijesus7_wei10-10 13:44
等级 T1 27楼

int[] nums = { 5, 6, 6, 6, 8, 8, 7 };
        HashSet<Integer> set = new HashSet<Integer>();
        for (int i : nums) {
            set.add(i);
        }

0 0
zxabc332335zxabc33233510-10 15:31
等级 T1 28楼

public static void main(String[] args) {
        String[] s = {“1”,”10”,”15”,”14”,”111”,”133”,”12”,”13”,”1”,”13”};
        List<String> l = new ArrayList<String>();
        for(String a:s){
            if(!l.contains(a)){
                l.add(a);
            }
        }
        System.out.println(l);
    }

0 0


0 0 回复 分享
img
微信分享新浪微博QQ好友QQ空间
取 消
(function(){
        $('#share_btn').click(function(){
            $('.popup_cover').stop().show();
            $('.sharePopup_box').stop().slideDown();
        });
        $('.sharePopup_cancel').click(function(){
            $('.popup_cover').stop().hide();
            $('.sharePopup_box').stop().slideUp();
        });
    });
('.topic-expired').click(function(){ alert("帖子太久远,不提供回复功能"); });





Zhangdragonfly
Zhangdragonfly


      <li><a href="/" class="J_nav"><i class="left_dot">•</i><span>首页</span></a></li>      <li><a href="javascript:;" class="J_nav"><i class="left_dot">•</i><span>我感兴趣的论坛</span><i class="iconfont icon_down"></i><i class="iconfont icon_up"></i></a>                <ul class="sub_nav">                 <li>

·基础类


  • ·疑难问题

  • ·非技术版

  • ·求职面试

  • ·职场话题

  • ·Web 开发

  • ·HTML(CSS)

  • ·JavaScript

  • ·Apache

  • ·Ajax

  • ·其他

  • ·HTML5

  • ·Linux/Unix社区

  • ·Oracle

  • ·Java

  • ·其他数据库开发

  • ·职场生涯

  • ·MS-SQL Server

  •       <li><a href="javascript:;" class="J_nav"><i class="left_dot">•</i><span>移动开发</span><i class="iconfont icon_down"></i><i class="iconfont icon_up"></i></a>


  • 云计算

  • 企业IT

  • .NET技术

  • Java 技术

  • Web 开发

  • PHP

  • VC/MFC

  • VB

  • Delphi

  • C++ Builder

  • C/C++

  • 其他开发语言

  • MS-SQL Server

  • PowerBuilder

  • Oracle

  • 其他数据库开发

  • Linux/Unix社区

  • Windows专区

  • 硬件/嵌入开发

  • 游戏开发

  • 网络与通信

  • 扩充话题

  • 挨踢职涯

  • 华为开发者社区

  • 软件工程/管理

  • 专题开发/技术/项目

  • 多媒体开发

  • 培训认证

  • 站务专区

  • GE Predix论坛

  •     </ul>  </div></div>


    #popup_mask { position: absolute; width: 100%; height: 100%; background: #000; z-index: 9999; left: 0px; top: 0px; opacity: 0.3; filter: alpha(opacity=30); display: none; }
    img即使是一小步
    也想与你分享
    打开
    img
    (function(){('#search_wap_topic').click(function(e) { console.log(12123123); e.preventDefault(); $('#search_wap_topics').submit(); }); });

    0 0
    原创粉丝点击