简体   繁体   中英

Storing number of 1's in binary representation of an int array in another array

I want to store the number of 1's in the binary representation of some integers given in an array in another corresponding array; following is the code I am writing; but it shows the error " Change type of 'arr' to 'int' " What's going wrong?

public static int[] arrange(int[] numbers){
String[] arr = new String[numbers.length];
for(int i =0;i<numbers.length;i++){
    arr[i]= Integer.toBinaryString(numbers[i]);
}
int[] a2 = new int[numbers.length];
for(int i =0;i<numbers.length;i++){
    a2[i]=Integer.bitCount(arr[i]);
}

As per Integer documentation , method bitCount requires an int and a String ,

Integer.bitCount(arr[i]); , arr[i] is String

You are passing a String to Integer.bitCount method:

a2[i]=Integer.bitCount(arr[i]);

But the method bitCount(int) is not applicable for the arguments (String). Change this assingment to pass int value to bitCount :

a2[i]=Integer.bitCount(Integer.parseInt(arr[i]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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