Java Array de-weight

Source: Internet
Author: User

Java Array de-weight

1. Create a new array for saving comparison results

2. Set the maximum minimum value of the random array

3. Start to go heavy

4. Calculate the time required to de-weigh

 Packageorg.zheng.collection;ImportJava.util.Random; Public classArraytest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//randomly create an array        int[] Test =New int[100000]; Random Random=NewRandom (); intmax = 100000000; //set the minimum value of the random array to 1        intMin = 1;  for(inti=0;i<test.length;i++){            ints = random.nextint (max)% (max-min+1) +min; Test[i]=s; }        LongStartTime =System.currenttimemillis (); //call the go-to-weight method        int[] res =Arraytest.uniquearr (test); LongEndTime = System.currenttimemillis ();//Get end TimeSystem.out.println ("New array Method program run time:" + (Endtime-starttime) + "MS"); LongStartTime2 =System.currenttimemillis (); //call the go-to-weight method        int[] Res2 =arraytest.uniquearr2 (test); LongEndTime2 = System.currenttimemillis ();//Get end TimeSystem.out.println ("Front Shift run Time:" + (endtime2-starttime2) + "MS"); }     Public Static int[] Uniquearr (int[] oldarr) {        //create a new array to save the data after comparison        int[] NewArr =New int[Oldarr.length]; newarr[0] = oldarr[0]; //set the tag for an array index        intindex = 1;  for(inti=1;i<oldarr.length;i++){            Booleanret=false;  for(intj=0;j<index;j++){                if(oldarr[i]==Newarr[j]) {ret=true;  Break; }            }            //does not have the same data, the new array adds a            if(ret==false) {Newarr[index]=Oldarr[i]; Index++; }        }        //copies the data of the new array to the nearest array for returning        int[] res =New int[index];  for(inti = 0; i<index;i++) {Res[i]=Newarr[i]; }        returnRes; }    //Front Shift Method     Public Static int[] UNIQUEARR2 (int[] arr) {        intindex = 1;  for(inti = 1; I <arr.length;i++){            BooleanRET =false;  for(intj = 0;j<index;j++){                if(arr[i]==Arr[j]) {ret=true;  Break; }            }            if(ret==false) {Arr[index]=Arr[i]; Index++; }                    }        int[] NewArr =New int[index];  for(inti = 0;i<index;i++) {Newarr[i]=Arr[i]; }        returnNEWARR; }}

  

Java Array de-weight

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.