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