如何修复以下问题,使其只能返回数组中不重复的值?

我有一个排序的整数数组,其中一些值重复,但我想返回不重复的值。例如,我有这个:


Integer[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};

它应该返回值-32700。但是,结果给了我 -32560 。


我的方法可能有什么问题?


这是我的代码:


    Integer[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};

    int n = arr.length;

    //to store repeating element

    int[] temp = new int[1];


    //if only one element return it

    if(arr.length==1)

    {

        System.out.println(arr[0]);

    }

    else

    {

        for(int j=1; j<n-1; j++)

        {   //compare current element with it's previous and subsequent

            if((arr[j-1]!=arr[j]) && (arr[j]!=arr[j+1]))

            {

                temp[0] = arr[j];

            }


        }

    System.out.println(temp[0]);

    }


繁花不似锦
浏览 105回答 4
4回答

慕标琳琳

返回 -32560 的原因是因为您尝试使用运算符比较数字对象 (&nbsp;Integer)&nbsp;!=,这几乎总是返回 true (因此返回最终值),因为数组中的所有整数在技术上并不相等(他们的对象引用会不同)。您可以通过更改行来解决此问题:if((arr[j-1]!=arr[j])&nbsp;&&&nbsp;(arr[j]!=arr[j+1]))到if(!(arr[j-1].equals(arr[j]))&nbsp;&&&nbsp;!(arr[j].equals(arr[j+1])))或者您可以将数组更改为原始数组int而不是数字类IntegerInteger[]&nbsp;arr&nbsp;=&nbsp;{-32735,&nbsp;-32735,&nbsp;-32700,&nbsp;-32645,&nbsp;-32645,&nbsp;-32560,&nbsp;-32560};到int[]&nbsp;arr&nbsp;=&nbsp;{-32735,&nbsp;-32735,&nbsp;-32700,&nbsp;-32645,&nbsp;-32645,&nbsp;-32560,&nbsp;-32560};两种方法都可以 - 希望这会有所帮助!

胡子哥哥

这里的问题是您正在比较 Integer 对象实例的相等性而不是值。只需获取您的代码,我就会在 IDE 中针对您正在使用的比较发出警告。如果您想与原始值进行比较,最简单的更改是将数组的声明更改为int[]&nbsp;arr&nbsp;=&nbsp;{-32735,&nbsp;-32735,&nbsp;-32700,&nbsp;-32645,&nbsp;-32645,&nbsp;-32560,&nbsp;-32560};这样做会产生预期的结果-32700如果您想让数组保留 Integer 对象,另一种解决方案是按照 Michael Michailidis 的建议进行操作,并在比较中使用 equals。

收到一只叮咚

运算符对 int 类型 (x==y) 或 Integer 类 (x.equals(y)) 执行的操作不同,以修复使用适当的整数运算符:public class Example {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp;int[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};&nbsp; &nbsp; &nbsp; &nbsp; for(int j=1; j<arr.length-1; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((arr[j-1]!=arr[j]) && (arr[j]!=arr[j+1]))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("int>"+arr[j]);&nbsp; &nbsp; Integer[] I_arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};&nbsp; &nbsp; &nbsp; &nbsp; for(int j=1; j<I_arr.length-1; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((!I_arr[j-1].equals(I_arr[j])) && (!I_arr[j].equals(I_arr[j+1])))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Interger>"+I_arr[j]);&nbsp; &nbsp; }}

繁星点点滴滴

&nbsp; &nbsp; &nbsp;Integer[] arr = {-32735, -32735, -32700, -32645, -32645, -32560, -32560};&nbsp; &nbsp; &nbsp; &nbsp; int n = arr.length;&nbsp; &nbsp; &nbsp; &nbsp; //to store repeating element&nbsp; &nbsp; &nbsp; &nbsp; int[] temp = new int[1];&nbsp; &nbsp; &nbsp; &nbsp; //if only one element return it&nbsp; &nbsp; &nbsp; &nbsp; if (arr.length == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(arr[0]);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j < n - 1; j++) {&nbsp; &nbsp;//compare current element with it's previous and subsequent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((!arr[j - 1].equals(arr[j])) && (!arr[j].equals(arr[j + 1]))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp[0] = arr[j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(temp[0]);&nbsp; &nbsp; &nbsp; &nbsp; }您的代码现在正在运行。问题在于您进行的对象比较。Integer是一个对象(包装器),而int是一个基元。==和=!在基元上效果很好,但在对象上效果不佳
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java