集合框架-去除ArrayList集合中的重复字符串元素
08-17
/**
* 去除ArrayList集合中重复的字符串
*/
package com.gc.test;
import java.util.ArrayList;
import java.util.Iterator;
public class StudentTest {
public static void main(String[] args) {
ArrayList arr = new ArrayList();
arr.add("hello");
arr.add("word");
arr.add("java");
arr.add("hello");
arr.add("java");
arr.add("word");
arr.add("java");
ArrayList newArr =new ArrayList(arr.size());
Iterator it = arr.iterator();
while (it.hasNext()) {
String type = (String) it.next();
if(!newArr.contains(type)){
newArr.add(type);
}
}
System.out.println(newArr);
}
}
结果:
[hello, word, java]