2014年5月22日星期四

java implementation issues array to heavy

Array want to achieve weight problems. Here is my method which is the use of 0 to replace duplicate values ​​, then press the non-zero output , to go heavy. Then I want to be able to achieve directly duplicate values ​​one is covered , such as 1123345678 ; which assembled a number 1 and 3 directly removed, the array it also shortens the length of two ?
specifically how to achieve it ? Confused for a long time

package test;
import java.util.Scanner;
public class Exercise6_51 {
public static void main(String args[]){
    int[] a=new int[10];
    Scanner input=new Scanner(System.in);
    System.out.print("Enter ten numbers:");
    for(int i=0;i<a.length;i++){
     a[i]=input.nextInt();
    }
    for(int i=0;i<a.length;i++){
     for(int j=i;j<a.length-1;j++){
     if(a[i]==a[j+1]){
     a[j+1]=0;
     }
     }
    }
    //java.util.Arrays.sort(a);
    System.out.print("The distinct numbers are:");
    for(int i=0;i<a.length;i++){
     if(a[i]!=0){
     System.out.print(a[i]+" ");
     }
    }
    
  }  
}


------ Solution ------------------------------------- -------

public static void main(String[] args) {

int[] arr = new int[] { 1, 1, 2, 3, 3, 4, 5, 6, 7, 8 };
Set<Integer> set = new TreeSet<Integer>();

for (int i : arr) {
set.add(i);
}

int[] des = new int[set.size()];
int j = 0;
for (Integer i : set) {
des[j++] = i;
}
System.out.println(Arrays.toString(des));
}

------ Solution --------------------------- ----------------- there is a problem
landlord method, if the array itself repeated 0 exist , the processing will get the wrong results
is a good way to use the set
If you want to implement your own words ,
conventional approach is to create a new array of primitive values ​​in the array into a new array, compare placed before
complexity is O (n * n)

------ For reference only ---------------------------------- -----
you can replace it with other collections , ah, in exchange for an array
------ For reference only ------------------ ---------------------
directly Set not on line yet ?
------ For reference only -------------------------------------- -

with Set is the easiest
------ For reference only ------------------------ ---------------
set not on ok with it?
Why do you want to write their own methods !

------ For reference only ---------------------------------- -----
you do not Set to write your own words , to achieve equals and hashcode to determine the specific details you can refer to Set
------ For reference only -------- -------------------------------

ah well , oh ... if true zero itself a problem ...
------ For reference only -------------------------------- -------

taught the ~ use TreeSet no duplicate elements characteristic to achieve the purpose
------ For reference only remove duplicate elements ------- --------------------------------

package test;
import java.util.Scanner;
public class Exercise6_51 {
public static void main(String args[]){
    int[] a=new int[10];
    int length=a.length;
    //int i,j,k;
    Scanner input=new Scanner(System.in);
    System.out.print("Enter ten numbers:");
    for(int i=0;i<a.length;i++){
     a[i]=input.nextInt();
    }
    for(int i=0;i<length;i++){
     for(int j=i;j<length-1;j++){
     if(a[i]==a[j+1]){
     for(int k=j+1;k<length-1;k++){
     a[k]=a[k+1];
     }
     length--;
     j--;
     }
     }
    }
    //java.util.Arrays.sort(a);
    System.out.print("The distinct numbers are:");
    for(int i=0;i<length;i++){
     System.out.print(a[i]+" ");
    }
  }  
}
这样就可以了,0的问题解决了,谢谢大家了

------ For reference only ---------------------------------------
you this code bug , oh well . . . .
------ For reference only -------------------------------------- -
package com.sesan.last;

import java.util.Scanner;

public class Exercise6_51 {
public static void main (String args []) {
int [] a = {1,3,4,5,3,2,5,6,4,3,2, 2,2,2,2,2,6 } ;
int length = a.length;
/ / int i, j, k;

for (int i = 0; i for (int j = i; j if (a [i] == a [j +1] ) {
for (int k = j +1; k a [k] = a [k +1];
for (int m = 0; m System.out.print (a [m] + "");
}
System.out.println (",");
}
length -;
/ / j -;
}
}
}
/ / java.util.Arrays.sort (a);
System.out.print ("The distinct numbers are:");
for (int i = 0; i System.out.print (a [i] + " ; " ) ;
}
}
}
There duplicate the results need further improvements The distinct numbers are: 1 3 4 5 2 6 2 2

没有评论:

发表评论