英文:
How to create a char[] using data from a boolean array?
问题
我有一个Boolean
数组,我想创建一个相应的字符数组,以便新数组中的每个true
对应一个1
,对于每个false
对应一个0
。这是我的代码,但新数组似乎是空的,因为没有输出任何内容,Boolean nums[]
的输出正常。
char[] digits = new char[n];
for (int i = 0; i < n; i++) {
if (nums[i]) {
digits[i] = '1';
}
else if (!nums[i]) {
digits[i] = '0';
}
}
for (int k = 0; k < n; k++) {
System.out.print(digits[k]);
}
英文:
I have a Boolean
array and I am trying to make a corresponding char array, so that to each true in the new array corresponds a 1 and for each false a 0. this is my code but it seems the new array is empty, because nothing prints, the Boolean nums[]
prints fine.
char[] digits = new char[n];
for (int i = 0; i < n; i++) {
if (nums[i]) {
digits[i] = 1;
}
else if (!nums[i]) {
digits[i] = 0;
}
}
for (int k = 0; k < n; k++) {
System.out.print (digits[k]);
}
答案1
得分: 11
Your problem is that you don't have quotes surrounding the 1
and 0
.
for (int i = 0; i < n; i++) {
if (nums[i]) {
digits[i] = '1';
}
else {
digits[i] = '0';
}
}
Without the quotes, they are cast from int
s to char
s. 0 is actually the null character (NUL
), and 1 is start of heading or something like that. Java chars are encoded using UTF-16 (they're 16 bits long). The characters '0' and '1' are actually encoded by 48 and 49 respectively (in decimal).
EDIT: Actually, don't look at the ASCII table, look at the Unicode character set. Unicode is really a superset of ASCII, but it'll probably be more useful than the ascii table.
英文:
Your problem is that you don't have quotes surrounding the 1
and 0
.
for (int i = 0; i < n; i++) {
if (nums[i]) {
digits[i] = '1';
}
else {
digits[i] = '0';
}
}
Without the quotes, they are cast from int
s to char
s. 0 is actually the null character (NUL
), and 1 is start of heading or something like that. Java chars are encoded using UTF-16 (they're 16 bits long). The characters '0' and '1' are actually encoded by 48 and 49 respectively (in decimal).
EDIT: Actually, don't look at the ASCII table, look at the Unicode character set. Unicode is really a superset of ASCII, but it'll probably be more useful than the ascii table
答案2
得分: 4
根据Oracle的Java教程中《学习Java语言》部分的“语言基础”课程中的原始数据类型:
char数据类型是一个单一的16位Unicode字符。它的最小值是'\u0000'(或0),最大值是'\uffff'(或65,535,包括在内)。
Unicode值0(零)是一个不可打印字符,就像Unicode值1(一)一样。这就是为什么您看不到任何输出。要么将digits
更改为int
数组,要么用字符文字(如'0'
或'1'
)填充它。
如果您使用int
数组,以下代码足够:
int[] digits = new int[n];
for (int i=0; i<n; i++) {
if (nums[i]) {
digits[i] = 1;
}
}
for (int k=0; k<n; k++) {
System.out.print(digits[k]);
}
请注意,int
数组会被隐式初始化,使得所有元素最初都是0(零)。
英文:
According to Primitive Data Types in the Language Basics lesson of trail Learning the Java Language in Oracle's Java tutorials:
>The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
Unicode value 0 (zero) is a non-printing character, as is unicode value 1 (one). That's why you aren't seeing anything printed. Either change digits
to a int
array or fill it with character literals such as '0'
or '1'
If you use int
array, the following code will suffice:
int[] digits = new int[n];
for (int i=0; i<n; i++) {
if (nums[i]) {
digits[i] = 1;
}
}
for (int k=0; k<n; k++) {
System.out.print (digits[k]);
}
Note that a int
array is implicitly initialized such that all the elements are initially 0 (zero).
答案3
得分: 3
你可以这样转换:
public static void main(String[] args) {
int n = 5;
boolean[] nums = { true, false, true, false, true };
char[] digits = new char[n];
for (int i = 0; i < n; i++) {
digits[i] = nums[i] ? '1' : '0';
}
}
英文:
You can convert like this:
public static void main(String[] args) {
int n = 5;
boolean[] nums = { true, false, true, false, true };
char[] digits = new char[n];
for (int i = 0; i < n; i++) {
digits[i] = nums[i] ? '1' : '0';
}
}
答案4
得分: 3
你可以做类似这样的事情
char[] myChars = new char[n/16];
for (int i = 0; i < nums.length/16; i++) {
String myChar = "";
for (int j = 0; j < 16; j++) {
if (nums[i*16+j])
myChar += "1";
else
myChar += "0";
}
myChars[i] = (char) Integer.parseInt(myChar, 2);
}
英文:
you can do something like that
char[] myChars = new char[n/16];
for(int i=0;i<nums.length/16;i++);{
String myChar ="";
for(int j=0;j<16;j++){
if(nums[i*16+j])
myChar+="1";
else
myChar+="0";
}
myChars[i]=Integer.parseInt(myChar,2);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论