Java中char数组的默认值是什么? [英] What are the default values of the char array in Java?

查看:458
本文介绍了Java中char数组的默认值是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我像这样分配字符数组:

If I allocate array of characters like this:

char[] buffer = new char[26];

分配的默认值是多少?我试图打印它,它只是一个空角色。

what are the default values it is allocated with? I tried to print it and it is just an empty character.

System.out.println("this is what is inside=>" + buffer[1]);

this is what is inside=>

是否有ASCII代码?
我需要能够确定数组是否为空,而且当我用字符填充前五个元素时,我需要找出第六个元素是空的。谢谢。

Is there an ASCII code for it? I need to be able to determine if the array is empty or not, and further on, when I fill say first five elements with chars, I need to find out that the sixth element is empty. Thanks.

推荐答案

它与任何类型相同:该类型的默认值。 (所以你在一个没有特别初始化的字段中也是如此。)

It's the same as for any type: the default value for that type. (So the same as you'd get in a field which isn't specifically initialized.)

默认值在JLS 4.12.5


对于char类型,默认值为空字符,即'\ u0000'

话虽如此,听起来确实你想要一个 List< Character> ,它可以跟踪集合的实际大小。如果你需要随机访问列表(例如,你希望能够填充元素25,即使你没有填充元素2),那么你可以考虑:

Having said that, it sounds like really you want a List<Character>, which can keep track of the actual size of the collection. If you need random access to the list (for example, you want to be able to populate element 25 even if you haven't populated element 2) then you could consider:


  • A 字符[] 使用 null 作为未设置值而不是'\ u0000'(毕竟,这仍然是一个角色......)

  • A Map< ;整数,字符>

  • 坚持 char [] 如果你知道你永远不会,永远,曾经想要将价值'\ u0000'的元素视为set

  • A Character[] using null as the "not set" value instead of '\u0000' (which is, after all, still a character...)
  • A Map<Integer, Character>
  • Sticking with char[] if you know you'll never, ever, ever want to consider an element with value '\u0000' as "set"

(如果不了解更多关于你正在做的事情,很难知道哪一个是最合适的。)

(It's hard to know which of these is the most appropriate without knowing more about what you're doing.)

这篇关于Java中char数组的默认值是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆