作为数据结构输入的填充零位

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

Padded zeroes as a part of an input for a data structure

问题

当我运行这段代码时:

LinkedList<Integer> list = new LinkedList<>();

System.out.println(list.isEmpty());

list.add(13);
list.add(423);
list.add(23);
list.add(022);
list.add(122);
list.add(25);

System.out.println(list.get(1));

System.out.println(list.isEmpty());

for (Integer i: list) {
    System.out.println(i);
}

我得到这个输出:

true
423
false
13
423
23
18
122
25

为什么 022 被转换成 18 呢?

英文:

So I created a custom linkedlist and while testing it to ensure functionality, I noticed something weird.

When I run this code:

    LinkedList&lt;Integer&gt; list = new LinkedList&lt;&gt;();
    
    System.out.println(list.isEmpty());
    
    list.add(13);
    list.add(423);
    list.add(23);
    list.add(022);
    list.add(122);
    list.add(25);
    
    System.out.println(list.get(1));
    
    System.out.println(list.isEmpty());
    
    for (Integer i: list) {
        System.out.println(i);
    }

I get this output:

true
423
false
13
423
23
18
122
25

Why is it that 022 got converted into 18?

答案1

得分: 2

因为它被视为一个八进制基数(8),因为该数字具有前导的0。所以,它对应的十进制值是18。

022

= (2 * 8 ^ 0) + (2 * 8 ^ 1)
= (2 * 1) + (2 * 8)
= 2 + 16
= 18

英文:

Because it's taken as an octal base (8) since that numeral has 0 in leading. So, it's corresponding decimal value is 18.

022

= (2 * 8 ^ 0) + (2 * 8 ^ 1)
= (2 * 1) + (2 * 8)
=  2 + 16
=  18  

huangapple
  • 本文由 发表于 2020年7月30日 13:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63167015.html
匿名

发表评论

匿名网友

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

确定