0

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]);
}
2

2 Answers 2

1

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]));
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.