編譯時下面的代碼,這是我接收到一個錯誤:上線10Leetcode220錯誤:無可比擬類型:int和<null>
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (nums == null || k <= 0 || t < 0 || nums.length < 2) return false;
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
int floor = set.floor(nums[i] + t);//return largest number smaller than nums[i] + t or null
int ceil = set.ceiling(nums[i] - t);//return least number larger than num[i] - t or null
if ((floor != null && floor >= nums[i]) || (ceil != null && ceil <= nums[i])) {
return true;
}
set.add(nums[i]);
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}}
「無比類型int和」,但如果我TreeSet中之前添加最終關鍵字,floor和ceil,代碼將被成功編譯。任何人都可以幫我解釋發生了什麼?謝謝。
...
final TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < nums.length; i++) {
final Integer floor = set.floor(nums[i] + t);//return largest number smaller than nums[i] + t or null
final Integer ceil = set.ceiling(nums[i] - t);//return least number larger than num[i] - t or null
...