- package com.heima.list;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class Test2 {
- public static void main (String[]args) {
- ArrayList list = new ArrayList ();
- list.add("a");
- list.add("a");
- list.add("b");
- list.add("b");
- list.add("c");
- list.add("c");
- System.out.println(list);
- ArrayList newList = getSingle(list) ;
- System.out.println(newList);
- }
- public static ArrayList getSingle (ArrayList list) {
- ArrayList newlist = new ArrayList ();
- Iterator it = list.iterator();
- while (it.hasNext()) {
- String temp = (String) it.next();
- if (!newlist.contains(temp)) {
- newlist.add(temp);
- }
- }
- return newlist;
-
- }
-
- }
复制代码 |
|