为什么我不能为BigInteger和int使用equals?

时间:2017-11-20 15:08:08

标签: java int bigint

当我尝试比较struct Node { bool is_Leaf = false; unordered_map<char,Node*> transitions; }; void insert(Node* node,char* str,char* end){ if(str==end) {node->is_Leaf = true;return ;} if (node->transitions[*str]==nullptr) node->transitions[*str] = new Node(); insert(node->transitions[*str],str+1,end); } void search(Node* root,char* str,char* end,string so_far=""){ if(root && root->is_Leaf) cout<<so_far<<endl; if(!root or str==end) return; so_far.push_back(*str); search(root->transitions[*str],str+1,end,so_far); } int main(int argc, char const *argv[]) { Node* root = new Node(); vector<string> prefixes = {"a", "ab", "ba", "aba", "bbb"}; for(auto& s:prefixes) insert(root,&s[0],&s[0]+s.size()); string to_search = "abad"; search(root,&to_search[0],&to_search[0]+to_search.size(),""); return 0; } BigInteger时:

int

我明白了:

  不可逆类型'int'和'BigInteger'

的对象之间的

equals()

2 个答案:

答案 0 :(得分:5)

使用

if (!balance.equals(BigInteger.ZERO)) {
    ...
}

答案 1 :(得分:4)

intBigInteger不共享任何类,因为int原始类型

您只能比较至少共同点的内容,例如Object。因此,像BigInteger和包装类Integer之类的比较会被编译,但结果将是false,因为Integer不是BigInteger。您需要将<{1}}转换int进行比较。

对于BigInteger常量 0documentation):

BigInteger.ZERO