Home > Java > javaTutorial > Detailed code examples for removing duplicate method sets from Java arrays

Detailed code examples for removing duplicate method sets from Java arrays

黄舟
Release: 2017-03-04 09:34:21
Original
1579 people have browsed it

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 + ",");  
        }  
    }  
}
Copy after login



## Run as follows:

之前的数组
4,2,4,6,1,2,4,7,8,
之后的数组
4,2,6,1,7,8,
Copy after login
The above is a detailed explanation of the code example for removing duplicate method sets from Java arrays. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template