Java配列重複要素の除去

2352 ワード

import java.util.LinkedList;
import java.util.List;
/*
 *  , :  {4,2,4,6,1,2,4,7,8}, {4,2,6,1,7,8}
 */
public class RemoveTest {

    public static void main(String[] args) {
        String[] arr = {"4","2","4","6","1","2","4","7","8"};
        //  set, set 
/*        Set<String> set = new TreeSet<>();
        set.addAll(Arrays.asList(arr)); 
        System.out.println(" :");
        System.out.println(" :"+set); //   Set 
*/
        //  list, 
        List<String> list = new LinkedList<>();
        for (int i = 0; i < arr.length; i++) {
            if ( !list.contains(arr[i])) {
                list.add(arr[i]);
            }
        }//  get list 
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i)+" ");
        }
    }
}