如何将数字添加到长值

How to add digits to a long value

所以我有这个变量 long hashvalue,如果某些事情是真的,我想向它添加数字。

我有这个数组 int arr[3][3],里面有数字 -1, 1, 0, 0, -1, 1, 0, 0, -1

我需要这样的东西:

if(array[i][j] == -1){
    //add digit 2 to hashvalue
} else if(array[i][j] == 1){
    //add digit 1 to hashvalue
} else if(array[i][j] == 0){
    //add digit 0 to hashvalue
} 

哈希值 = 210021002

您可以简单地为散列值创建一个字符串,然后通过 +(或 +=)运算符将数字添加到字符串中。要从字符串中接收长对象,可以使用 Long.parseLong() 函数。

String hashvalue = "";

// Add your digits to the String here
hashvalue += "1";

// Convert the String to a long
long finalHashvalue = Long.parseLong(hashvalue);

您可以将哈希值乘以 10,然后添加所需的数字。 喜欢:

if(array[i][j] == -1){
    hashValue = hashValue * 10 + 2;
} else if(array[i][j] == 1){
    hashValue = hashValue * 10 + 1;
} else if(array[i][j] == 0){
    hashValue = hashValue * 10 + 0;
} 

假设你的hashValue以3开头,如果你把它乘以10就变成30,如果你加上数字,比如说2,它就变成32。这个数字已经被添加了。希望对您有所帮助!