返回顶部

收藏

java list用法,去除字符串数组中重复值

更多

ArrayTest.java

import java.util.Arrays;
import java.util.List;
import java.util.LinkedList;

public class ArrayTest {

    public static List printMap1(String[] s) {
        List<String> list =  new LinkedList<String>();
        List<String> list2 = Arrays.asList(s);
        list.addAll(list2);
        return list;

    }

    public static String[] array_unique(String[] a) {
        // array_unique
        List<String> list = new LinkedList<String>();
        for(int i = 0; i < a.length; i++) {
            if(!list.contains(a[i])) {
                list.add(a[i]);
            }
        }
        return (String[])list.toArray(new String[list.size()]);
    }

    public static void main(String[] args) {
        String[] ar = {"dd","cc","ee","ff","gg","cc","ee"};
        String[] s = array_unique(ar);
        for(String aa : s) {
            System.out.println(aa);
        }
        List list3 = printMap1(ar);
        for(Object o : list3) {
            System.out.println(o);
        }
    }   
} 

标签:数组,ArrayList

收藏

0人收藏

支持

0

反对

0

发表评论