英文:
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<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);
}
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论