Reputation: 197
I made this program to receive as an input a list of numbers and it needs to find the number that does not have a pair and it works perfectly fine with small inputs but when I give a larger input(more than 256) something really strange happens.The condition that checks each pair of numbers starts to say that two equal numbers are not the same :/ I really have no idea why. Does anyone have any idea?
import java.util.Scanner;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
public class HelpRequired{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int singleton=0;
int N;
N = s.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i<N; i++){
list.add(s.nextInt());
}
Collections.sort(list);
for(int i = 0;i<list.size();i+=2){
System.out.println("Comparing "+list.get(i)+" with "+list.get(i+1)+" "+(list.get(i)!=list.get(i+1)));
if(list.get(i)!=list.get(i+1)){ /*This starts to say that, for example 128!=128 is true and I have no idea why*/
singleton = list.get(i);
break;
}
}
pw.printf("%d\n",singleton);
pw.flush();
}
}
This is a fragment of the input file :
73
73
74
74
75
75
76
76
77
77
78
78
79
79
80
80
And this is a fragment of the output produced:
Comparing 116 with 116 false
Comparing 117 with 117 false
Comparing 118 with 118 false
Comparing 119 with 119 false
Comparing 120 with 120 false
Comparing 121 with 121 false
Comparing 122 with 122 false
Comparing 123 with 123 false
Comparing 124 with 124 false
Comparing 125 with 125 false
Comparing 126 with 126 false
Comparing 127 with 127 false
Comparing 128 with 128 true
Upvotes: 1
Views: 83
Reputation: 7110
Since you can't use primitive values (e.g. int
) in collections, Java wraps them in an Integer
object for you. This is called autoboxing: http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
When you compare the two numbers you are using ==
so you are comparing object identity and not object value. In your example, the two values of 128 are being represented by different Integer
objects so ==
is false.
Java has a feature where, when representing a value in the range -127 to 127 you are guaranteed to get the same Integer
object for the same value. This mean that reference equality will work in that range, but it won't necessarily work outside of that range.
From the language spec:
If the value p being boxed is ... an int between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
To solve your problem, you should use list.get(i).intValue() = ...
or list.get(i).equals(...)
instead.
Upvotes: 2
Reputation: 2866
public class HelpRequired {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int singleton = 0;
int N;
N = s.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
list.add(s.nextInt());
}
Collections.sort(list);
for (int i = 0; i < list.size(); i += 2) {
System.out.println("Comparing " + list.get(i) + " with "
+ list.get(i + 1) + " " +(list.get(i).equals( list.get(i + 1))));
if (list.get(i).equals( list.get(i + 1))) { /*
* This starts to say that, for
* example 128!=128 is true and
* I have no idea why
*/
singleton = list.get(i);
break;
}
}
pw.printf("%d\n", singleton);
pw.flush();
}
}
Try above. Error was in the comparison. You are adding input values to the list. Those number are being autoboxed to Integer. And to compare wrapper classes you should use equals method as has been done in above code.
Upvotes: 0