1 package cn.arraylist.com; 2 3 import java.util.ArrayList; 4 import java.util.Iterator; 5 6 public class ArrayListDemo2 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub10 ArrayList arraylist = new ArrayList();11 arraylist.add("li");12 arraylist.add("liu");13 arraylist.add("huang");14 arraylist.add("li");15 arraylist.add("huang");16 17 // 排序的方法去重复18 for (int x = 0; x < arraylist.size() - 1; x++) { // 当索引值x=0,依次和内循环进行对比,x=0的时候19 for (int y = x + 1; y < arraylist.size(); y++) { // y=x+1,每次x对比都从后面一个索引开始进行20 if (arraylist.get(x).equals(arraylist.get(y))) { // 获取到x和y的值进行对比21 arraylist.remove(y);22 }23 }24 }25 // 迭代器26 Iterator it = arraylist.iterator();27 String[] ss = {};28 while (it.hasNext()) {29 String s = (String) it.next();30 System.out.println(s);31 32 }33 34 }35 36 }