我做了這個程序接收一個數字列表作爲輸入,它需要找到沒有一對的數字,它用小輸入完美地工作,但是當我給一個更大的輸入(超過256)有些奇怪的事情發生。檢查每一對數字的條件開始說,兩個相同的數字是不一樣的:/我真的不知道爲什麼。有人有什麼主意嗎?ArrayList值的差異
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();
}
}
這是輸入文件的片段:
73
73
74
74
75
75
76
76
77
77
78
78
79
79
80
80
而這是產生的輸出的一個片段:
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
您的回答非常有幫助,我非常感謝您的解釋細節。我想到了自動裝箱,但對我來說,以前的數值確實符合條件。非常感謝。 – Darwin57721