如何使用布尔数组中的数据创建一个char[]?

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

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 &lt; n; i++) {
	if (nums[i]) {
		digits[i] = 1;
	}
	else if (!nums[i]) {
		digits[i] = 0;
    }
}
for (int k = 0; k &lt; 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 ints to chars. 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 &lt; n; i++) {
    if (nums[i]) {
        digits[i] = &#39;1&#39;;
    }
    else {
        digits[i] = &#39;0&#39;;
    }
}

Without the quotes, they are cast from ints to chars. 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 &#39;0&#39; or &#39;1&#39;

If you use int array, the following code will suffice:

int[] digits = new int[n];
for (int i=0; i&lt;n; i++) {
    if (nums[i]) {
        digits[i] = 1;
    }
}

for (int k=0; k&lt;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 &lt; n; i++) {
            digits[i] = nums[i] ? &#39;1&#39; : &#39;0&#39;;
        }
    }

答案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&lt;nums.length/16;i++);{
  String myChar =&quot;&quot;;
  for(int j=0;j&lt;16;j++){
     if(nums[i*16+j])
        myChar+=&quot;1&quot;;
     else
        myChar+=&quot;0&quot;;
  }
 myChars[i]=Integer.parseInt(myChar,2);
 }

huangapple
  • 本文由 发表于 2020年5月4日 03:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580341.html
匿名

发表评论

匿名网友

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

确定