0

让用户在 main 方法中向数组中输入 5 个整数值。将数组传递给单独的函数。在那里,检查数组中的重复值。如果发现重复,则消除重复的整数并将其替换为 -1。在主方法中打印处理后的数组。我想我知道如何用 -1 替换该值,但是如何再次将数组返回到主方法。代码是:

 package methods;
import java.util.*;
public class remdup {
    public static void main (String[]args) {
    Scanner in = new Scanner (System.in);
    System.out.println("Enter 5 integers:  ");
    int [] x= new int [5];
    for (int i=0; i<5; i++) {
    x[i]=in.nextInt();
}
    check(x);
   // Insert method here 
}
    //Method check starts here...
    public static void check(int []y) {
      //  int pos = y[0];
        int len=y.length;
        int i=0;
        int j = i+1;
       for (i=0; i<len-1; i++) {
           for (j=i+1; j<len-1;j++ ) {
            if (y[i]==y[j]) {
                //how to replace numbers???
               y[i]=-1;
                System.out.println("Duplicate found");
            }
            }
        } 

    }
}
4

6 回答 6

3

使用 Set 来跟踪您已经拥有的数字。遍历您的数组并检查该集合是否包含您当前位置的数字。如果是:将其替换为 -1。如果否:将数字添加到集合中...

public static void check(int []y) {
    Set<Integer> foundNumbers = new HashSet<Integer>();

    for(int index = 0; index < y.length; index++) {
        if(foundNumbers.contains(y[index]) {
            y[index] = -1;
        } else {
            foundNumbers.add(y[index]);
        }
    }
}
于 2013-09-10T09:20:09.140 回答
0

你像这样替换数组中的数字:

y[i] = -1;

但是您的检查功能不会以这种方式工作。

于 2013-09-10T09:15:37.987 回答
0

check由于这是家庭作业,但是如果您查看您的功能,我不会详细介绍:

public static void check(int []y) {
      //  int pos = y[0];
        int len=y.length;
        int i=0;
        int j = i+1;
       for (i=0; i<len-1; i++) {
            if (y[i]==y[j]) {
                //how to replace numbers???
                System.out.println("Duplicate found");
            }
        } 

    }

您会注意到您最初设置j为 1,但您从未更新其值。因此,在您的for循环中,您需要j在每次迭代结束时更新 的值。

您还需要包含一个额外的循环,一个保存当前数字,另一个检查其余数字。最后,要覆盖您拥有的值,只需像这样写入数组y[i] = -1

于 2013-09-10T09:16:02.873 回答
0

像这样改变你的整体

   import java.util.*;
    public class test1 {
        public static void main (String[]args) {
        Scanner in = new Scanner (System.in);
        System.out.println("Enter 5 integers:  ");
        int [] x= new int [5];
        for (int i=0; i<5; i++) {
        x[i]=in.nextInt();
    }
        int z[]=check(x);
        for(int o=0;o<z.length;o++)
        {
            System.out.println(z[o]);
        }
       // Insert method here 
    }
        //Method check starts here...
        public static int[] check(int []y) {
          //  int pos = y[0];
            int len=y.length;
            int i=0;
            //int j = i+1;
           for (i=0; i<len; i++) {
               for(int j=0;j<i;j++)
               {
                if (y[i]==y[j]) {
                    //how to replace numbers???
                    y[i]=-1;
                   // System.out.println("Duplicate found");
                }
            } }
           for(int k=0;k<len;k++)
           {
               //System.out.println(y[k]);
           }
return y;
        }

}
于 2013-09-10T09:17:40.710 回答
0

这对你有用。

    public static void main(String[] args){
    Scanner in = new Scanner (System.in);
    System.out.println("Enter 5 integers:  ");
    Map<Integer,Integer> map=new HashMap<>();
    int [] x= new int [5];
    for (int i=0; i<5; i++) {
        int val=in.nextInt();
        x[i]=val;
        Integer iniVal=map.get(val);
        if(iniVal==null){
             iniVal=0;
        }
        map.put(val,iniVal+1);
    }
    int[] y=getNewArr(x,map);
    for (int i=0;i<y.length;i++){
        System.out.println(y[i]);
    }
}
public static int[] getNewArr(int[] x,Map<Integer,Integer> map){
    for(int i=0;i<x.length;i++){
        int numElement=map.get(x[i]);
        if(numElement!=1){
          for(int j=0;j<i;j++){
             if(x[i]==x[j]){
                    x[i]=-1;
             }
          }
        }
    }
     return x;
}

输入数组:{1,4,5,1,2}

输出数组:{1,4,5,-1,2}

于 2013-09-10T09:35:31.697 回答
0

尽管这是一篇非常古老的帖子,但我认为已接受的答案存在错误。我们应该将替换后的数字添加回数组。然而,在接受的答案中,我们将其替换为 -1,但我们从未将其添加回 SET。以下是我更正的解决方案:

    public static Set<Integer> check(int []y) {
        Set<Integer> foundNumbers = new HashSet<Integer>();
    
        for(int index = 0; index < y.length; index++) {
            if(foundNumbers.contains(y[index])){
                y[index] = -1;      
               
            } 
            
            foundNumbers.add(y[index]);
        }
         return foundNumbers;
        
    }
于 2022-02-09T03:45:09.150 回答