Java array removes repeated method sets, and java array duplicates

Source: Internet
Author: User

Java array removes repeated method sets, and java array duplicates

It is often used. Sometimes it is not just a simple basic type. The set can be used to remove duplicates. We have used custom types for a lot of time, here is an example (I will use int here ):

Method 1. This is similar to the selection sorting algorithm. First we take the I value and then remove all duplicates after I. The specific implementation is as follows:

Import java. util. list; import java. util. concurrent. copyOnWriteArrayList;/*** Creation Time: 3:26:35 ** @ author zhangtianyou * @ version 2.2 */public class ArrayRemoveRepeat {/*** @ param args */public static void main (String [] args) {// The original array is {, 8, 8,}. The result is }. System. out. println ("previous array"); 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 ("array after \ n"); for (Integer k: src) {System. out. print (k + ",");}}}

Run the following command:

Previous Array
, 8,
Array after
4,2, 6,1, 7,8,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.