首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

除去数组中的重复元素(引用类库hashset)

2012-08-25 
去除数组中的重复元素(引用类库hashset)今天遇到这样一个问题,就是需要去除数组中的重复元素,到网上查了下

去除数组中的重复元素(引用类库hashset)

今天遇到这样一个问题,就是需要去除数组中的重复元素,到网上查了下,用hashset觉得很方便:

package com.example;import java.util.Arrays;import java.util.HashSet;import java.util.Set;/** This class is a Java tutorial for detect and remove duplicate array elements*/public class Test{ /** This method shows how to check and delete duplicate array elements*/    public static void main(String[] args) {        String[] str = new String[10];          //Initializing the array with duplicate elements        for(int i=0;i<10;i++){            str[i] = "str";        }          //remove duplicates here        Set s = new HashSet(Arrays.asList(str));          System.out.println(s);    }}

?不过这样不一定能保存原来顺序了,如果要保持得用LinkedHashSet,最后toArray()就可以了

热点排行