日期:2014-05-20 浏览次数:20882 次
List<Integer> l1 = new ArrayList<Integer>();
List<Integer> l2 = new ArrayList<Integer>();
for (int i = 1; i < 7; i++) {
l1.add(i);
}
for (int i = 4; i < 9; i++) {
l2.add(i);
}
l1.addAll(l2);
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = l1.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
for (Object o : newList) {
System.out.print(o);
}
System.out.println("\n---");
List<Integer> listInt = new ArrayList<Integer>(
new LinkedHashSet<Integer>(l1));
for (Integer o : listInt) {
System.out.print(o);
}
public static void main(String args[]) throws Exception{
List list1=new ArrayList();
List list2=new ArrayList();
list1.add("1");
list1.add("2");
list2.add("2");
List<String> newChar=new ArrayList();
concat(newChar,list1,list2);
for(int i=0;i<newChar.size();i++){
System.out.println(newChar.get(i));
}
}
static List concat(List<String> newChar,List<String> list1,List<String> list2){
String[] char1=list1.toArray(new String[0]);
String[] char2=list2.toArray(new String[0]);
for(int i=0;i<char1.length;i++){
newChar.add(char1[i]);
}
for(int j=0;j<char2.length;j++){
if(!newChar.contains(char2[j])) newChar.add(char2[j]);
else {
System.out.println("有重复元素,不能拼接");
newChar.removeAll(newChar);
break;
}
}
return newChar;
}