示例代码:
public static void main(String[] args){。
List<Integer> list1 = new ArrayList<Integer>();。
list1.add(1);。
list1.add(2);。
list1.add(3);。
list1.add(4);。
List<Integer> list2 = new ArrayList<Integer>();。
list2.add(1);。
list2.add(4);。
list2.add(7);。
list2.add(10);。
List<Integer> listAll = new ArrayList<Integer>();。
listAll.addAll(list1);。
listAll.addAll(list2);。
listAll = new ArrayList<Integer>(new LinkedHashSet<>(listAll));。
System.out.println(listAll);。
}
输出:
[1, 2, 3, 4, 7, 10]。
代码要典:
合并 使用java.util.List.addAll(Collection<? extends Integer>)。
去重,借助LinkedHashSet 。
public static void main(String args[]){。
/**
* 大体思路:
* 1.把list2添加到list1中;添加时,先循环list2,用list2中的每个值,分别和。
* list1去比较,如果重复,跳出循环,则不添加,如果不重复,则list2的值添加到list1中。
*/
List<Integer> list2=new ArrayList<Integer>(); 。
list2.add(1); 。
list2.add(2); 。
list2.add(4); 。
List<Integer> list1=new ArrayList<Integer>(); 。
list1.add(1); 。
list1.add(2); 。
list1.add(5); 。
temp = 0;//重复标识 0-该值在list1中唯一;1-该值在list1中有重复值。
for(int i=0 ;i < list2.size();i++){。
for(int j=0; j< list1.size(); j++){。
if (list2.get(i) == list1.get(j)){。
temp = 1;。
}
}
if(temp = 0){。
list1.add(list2.get(i));。
}
}
System.out.println(list1.size());。
后记:list1本身也需要去重,方法一样,自己去一下吧。
遍历这两个list ,使用 双层循环,在内循环判断,若外循环的list的value存在在内循环中,则同时删除两个list中的那个value,不存在的话,使用第三个list来动态添加,最后返回第三个list就是你所需要的resultList。
import java.util.*;。
public class Test {。
public static void main(String[] args){ 。
List <Integer> l1=new ArrayList<Integer>();。
l1.add(1);l1.add(2);l1.add(3);。
l1.add(4);l1.add(5);l1.add(6);。
List <Integer> l2=new ArrayList<Integer>();。
l2.add(4);l2.add(5);l2.add(6);。
l2.add(7);l2.add(8);l2.add(9);。
Set<Integer> s=new TreeSet(l1);。
for(Integer i:l2){。
//当添加不成功的时候 说明s中已经存在该对象,直接remove掉该对象即可。
if(!s.add(i)) s.remove(i);。
}
System.out.println(s);。
//还可以用下面一种方法
List <Integer> temp=new ArrayList<Integer>(l1);//用来保存两者共同有的数据。
temp.retainAll(l2);。
l1.removeAll(temp);//l1中去掉两者共同有的数据。
l2.removeAll(temp);//l2中去掉两者共同有的数据。
List <Integer> l3=new ArrayList<Integer>();。
l3.addAll(l1);。
l3.addAll(l2);。
System.out.println(l3);。
}
package com.ajax.test;。
import java.io.File;。
import java.io.FileWriter;。
import java.util.Arrays;。
/**
* 把一个数组的元素复制到另个数组; 去除重复元素不能用SET集合; 每次复制的记录输到一个文件里。
*
* @author ajax_2003。
* @version 1.0, 2009-7-26。
*
*/
public class CopyArrayAndRemoveDuplicate {。
private static String FILE_PATH = "d:/abc.txt";。
private static File file;。
static {
file = new File(FILE_PATH);。
}
/**
* 取出冗余数据
*
* @param nums。
* 原数组。
*/
private int[] removeDuplicate(int[] nums) throws Exception {。
int[] tmpArray = new int[nums.length];。
int count = 0;。
loop: //
for (int i = 0; i < nums.length; i++) {。
int tmp = nums[i];。
for (int j = 0; j < count; j++) {。
if (tmp == tmpArray[j])。
continue loop;。
}
tmpArray[count++] = tmp;。
log("成功复制了元素" + tmp);// 写日志。
}
return copyArray(tmpArray, 0, count);。
}
/**
* 拷贝数组
*
* @param srcArray。
* 要拷贝的数组。
* @param startIndex。
* 拷贝起始索引。
* @param endIndex。
* 拷贝结束索引。
* @return 结果数组。
*/
private int[] copyArray(int[] srcArray, int startIndex, int endIndex)。
throws Exception {。
if (endIndex <= startIndex)。
throw new Exception("Argumens wrong!");。
int[] desArray = new int[endIndex - startIndex];。
System.arraycopy(srcArray, startIndex, desArray, 0, desArray.length);。
return desArray;。
}
/**
* 输出操作日志(即: 每次复制的记录输到一个文件里)。
*
* @param oprate。
*/
private void log(String oprate) {。
FileWriter out = null;。
try {
out = new FileWriter(file, true);。
out.write(oprate + "\r\n");。
} catch (Exception e) {。
e.printStackTrace();。
} finally {
try {
if (out != null)。
out.close();。
out = null;。
} catch (Exception ex) {。
ex.printStackTrace();。
}
}
}
public static void main(String[] args) {。
int[] nums = { 1, 223, 1, 2, 2, 2, 3, 2, 3, 34, 45, 5, 5, 3, 23, 2, 2,。
3, 4, 5, 5, 6, 7, 78, 8, 9, 34, 90, 45, 675, 4, };。
int[] finalArray;。
try {
finalArray = new CopyArrayAndRemoveDuplicate()。
.removeDuplicate(nums);。
System.out.println(Arrays.toString(finalArray));。
} catch (Exception e) {。
e.printStackTrace();。
}
}
有些地方可能考虑的不完全,见谅!
//合并数组并排序
public void arrCopy() {。
int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };。
int[] b = { 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000 };。
System.arraycopy(a, 2, b, 5, 5);。
for(int n : b){。
System.out.print(n+",");。
}
System.out.println();。
//去掉重复元素
public static void main(String args[]) { 。
int[] arr = { 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7, 8, 9, 9, 10, 11, 11, 。
11, 12, 12, 13, 14, 14, 15 }; // 预设数据数组 。
int index = 1; // 保存最后一个不重复的位置 。
int last = arr[0]; 。
for (int i = 1; i < arr.length; i++) { 。
if (arr[i] != last) { 。
arr[index] = arr[i]; 。
last = arr[index]; 。
index++; 。
}
}
int[] rtn = new int[index]; 。
System.arraycopy(arr, 0, rtn, 0, index); 。
System.out.println(Arrays.toString(rtn)); 。
}
原文地址:http://www.qianchusai.com/%E5%90%88%E5%B9%B6%E5%A4%9A%E4%B8%AAlist%E5%B9%B6%E5%8E%BB%E9%87%8D.html