Frequently used, sometimes not just simple basic types, but those that can be duplicated using set collections. Many times we use our custom types. Here is an example (I am using int as an example here) :
Method 1. This is similar to the selection sort algorithm. First we take the value of i, and then remove all duplicates after i. The specific implementation is as follows:
import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; /** * 创建时间:2014-11-18 下午3:26:35 * * @author zhangtianyou * @version 2.2 */ public class ArrayRemoveRepeat { /** * @param args */ public static void main(String[] args) { // 原始数组是{4,2,4,6,1,2,4,7,8, 8, 19,11},得到结果{4,2,6,1,7,8}。 System.out.println("之前的数组"); Integer[] src = { 4, 2, 4, 6, 1, 2, 4, 7, 8 }; for (Integer k : src) { System.out.print(k + ","); } List<Integer> list = new CopyOnWriteArrayList<Integer>(src); int i = 0; while (i < list.size() - 1) { int j = i+1; while (j < list.size()) { if (list.get(i) == list.get(j)) { list.remove(j); j--; } j++; } i++; } src = list.toArray(new Integer[list.size()]); System.out.println("\n之后的数组"); for (Integer k : src) { System.out.print(k + ","); } } }
## Run as follows:
之前的数组 4,2,4,6,1,2,4,7,8, 之后的数组 4,2,6,1,7,8,