如何将零存储在字符数组中,而不是存储空值 [JAVA]

huangapple go评论82阅读模式
英文:

How to store zero in a char array instead of it storing null [JAVA]

问题

抱歉可能问题表达不清晰并且知识有所不足。

我试图将实际字符 "0" 存储在数组中,但它总是默认为空。有没有办法避免这种情况?

以下是我的代码的修改版本。我尝试比较两个二进制数,当数字不匹配时,接下来的数字都变成了零。非常感谢任何帮助。

String fullBinOne = "1010101010100101";
String fullBinTwo = "1010101010101010";
char[] charBinOne = fullBinOne.toCharArray();
char[] charBinTwo = fullBinTwo.toCharArray();
char[] routeSummaryArray = fullBinOne.toCharArray();

int subnetCIDR = 0;

for (int i = 0; i < charBinOne.length; i++){
    if (charBinOne[i] != charBinTwo[i]) {
        subnetCIDR = i;
        break;
    }
}

for (int i = subnetCIDR; i < charBinOne.length; i++){
    routeSummaryArray[i] = (char)0;
}

routeSummaryArray 的输出是 "101010101010 ",而不是应该的 "1010101010100000"。

英文:

Apologies for the possibly poorly written question and lack of knowledge.

Im trying to store the actual character "0" in an array but it keeps defaulting to null. Is there anyway to avoid this?
Below is a modified version of my code. I am trying to compare two binary numbers and then when the numbers dont match, the following numbers become all zeroes. Any help would be greatly appreciated

        String fullBinOne = &quot;1010101010100101&quot;
        String fullBinTwo = &quot;1010101010101010&quot;
        char[] charBinOne = fullBinOne.toCharArray();
        char[] charBinTwo = fullBinTwo.toCharArray();
        char[] routeSummaryArray = fullBinOne.toCharArray();

        int subnetCIDR = 0;

        for (int i = 0; i &lt; charBinOne.length; i++){
            if (charBinOne[i] != charBinTwo[i]) {
                subnetCIDR = i;
                break;
            }
        }

        for (int i = subnetCIDR; i &lt; charBinOne.length; i++){
            routeSummaryArray[i] = (char)0;
        }

The output of routeSummaryArray is "101010101010 "
instead of being 1010101010100000

答案1

得分: 1

routeSummaryArray[i] = (char)0;

(char)0 是将整数值(这里是0)强制转换为字符。在这里,整数值(这里是0)的ASCII值将被存储到名为"routeSummaryArray"的字符数组的指定索引处。

0的ASCII值是null。所以输出为"101010101010"。

如果您需要输出为1010101010100000,请使用以下之一:

routeSummaryArray[i] = '0';

或者,

routeSummaryArray[i] = (char)48;

'0' 是ASCII值48的表示。

供参考,http://www.asciitable.com/

英文:
routeSummaryArray[i] = (char)0;

(char)0 is typecasting the integer value(here 0) to character. Here, the ASCII value of the integer value (here 0) will be stored to the specified index of the character array named "routeSummaryArray".

ASCII value of 0 is null. So it outputs as "101010101010".

If you need output as 1010101010100000, either use

routeSummaryArray[i] = &#39;0&#39;;

or,

routeSummaryArray[i] = (char)48;

'0' is the ASCII value of 48".

For your reference, http://www.asciitable.com/

huangapple
  • 本文由 发表于 2020年7月29日 16:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63149028.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定