简体   繁体   English

将char数组转换为int数组而在Java中没有循环?

[英]Converting char array to int array without a loop in Java?

I need to get the binary representation for a range of numbers inside a matrix in order to perform some operations with another vector. 我需要获取矩阵内一系列数字的二进制表示形式,以便对另一个向量执行某些操作。

So let's say I will get the binary representation for 2^4 numbers, that's it from 0 to 15. I know I need a 16x4 matrix. 假设我将获得2 ^ 4数字的二进制表示形式,即从0到15。我知道我需要一个16x4的矩阵。

I've got this code: 我有以下代码:

int [][] a = new int[15][4];

for (int i = 0; i < a.length; i++) {
    a[i] = String.format("%5s", Integer.toBinaryString(i)).replace(' ', '0').toCharArray();
}

So, being the array representation of the binary formatted number a char[] , I can't just asign it to a[i] . 因此,作为二进制格式的数字char[]的数组表示形式,我不能仅仅将其赋给a[i]

If there any way to perform a cast without looping through the char array? 是否有任何方法可以在不循环通过char数组的情况下执行转换?

Not that I am aware of. 不是我知道的。 There are some different ways you can do it, either looping through the integer representation of the binary string, and the taking num%10 and num/10 for every step, if you absolutely don't want a loop through the char array. 如果您绝对不希望遍历char数组,则可以通过几种不同的方法来执行此操作,即遍历二进制字符串的整数表示形式,并为每一步取num%10num/10 However in this case it seems pretty straight forward to just loop through the char array. 但是,在这种情况下,循环遍历char数组似乎很简单。 Anyways here is the solution, in the way you didn't want it I guess... 无论如何,这里是解决方案,我猜你以您不想要的方式...

int [][] a = new int[16][4];

for (int i = 0; i < a.length; i++) {
    char[] cArr = String.format("%4s", Integer.toBinaryString(i)).replace(' ', '0').toCharArray();
    for(int j = 0; j < a[0].length; j++)
        a[i][j] = Integer.parseInt(cArr[j]+"");

}

This is a simpler solution to what you are trying to accomplish... 这是您要完成的工作的简单解决方案。

    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[0].length; j++) {
            a[i][a[0].length - 1 - j] = (i & (1 << j)) != 0 ? 1 : 0;
        }
    }

Instead of converting an integer i to String and then replacing white spaces with zeros and then converting it to array, you: 您可以执行以下操作,而不是将整数i转换为String,然后将零替换为零,然后再将其转换为array:

  1. Take i. 拿我

  2. Take a binary number A with the only 1 at j-th position (other being zeros): A = (1 << j) 取一个二进制数A,在第j个位置上唯一的1(其他为零):A =(1 << j)

  3. Perform conjunction (binary bit-wise multiplication) of your number and the number A. This is accomplished by: (i & A) 对您的数字和数字A进行相加(二进制按位乘法)。这可以通过以下方式实现:(i&A)

  4. If there was non-zero bit at that position, after conjunction you will get A. If there was a zero bit, you will get 0. 如果该位置有一个非零位,则在相加后将得到A。如果有零位,则将得到0。

  5. If the result is not zero, i has non-zero bit in j-th position. 如果结果不为零,则我在第j个位置具有非零位。 Otherwise it has zero there. 否则,那里零。

The solution using bit-wise operations will be faster too. 使用按位运算的解决方案也将更快。

I believe that one outer loop will still be required to iterate through char[][] rows . 我相信仍然需要一个外部循环来遍历char[][] rows

int[] charArray2intArray(char[][] binary) {

    int[] numbers = new int[binary.length];
    int row = 0;
    for (char[] number: binary) {

        String bin = new String(number);
        numbers[row++] = Integer.parseInt(bin, 2);
    }

    return numbers;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM