How to change Boolean array to double array

135 views Asked by At

I have Boolean Array and I want to change them to double array. If a position is true,then double be 1, else set equal to 0. how can I do that?

my Boolean array:

        Boolean [] a = new Boolean [10];
        int i = 2;
        for(int x = 1; x < 7; x=x+i){
            String name = "b" + String.valueOf(x);
            a [x] = shared.getBoolean(name, false);}

my double array:

double [] r = new double[10];
2

There are 2 answers

0
theMfromA On BEST ANSWER

You can use a for-loop to fill the double-Array again:

    Boolean [] a = new Boolean [10];
    double [] r = new double[10];
    for(int i=0; i<a.length; i++) {
        if(a[i] != null && a[i])
            r[i] = 1d;
        else
            r[i] = 0d;
    }

As you see, I'm checking if Boolean a[i] != null, too. This is because you are using the Wrapper-Class Boolean (with capitalized B) of the datatype boolean. In this case, a[i] could be null! So you should check this somewhere to avoid Exceptions.

0
moffeltje On

I have no clue what you would like to achieve with your current code, but here's an example:

Boolean [] a = new Boolean[10];
//fill the array a
double [] r = new double[10];

for(int x = 0; x < 10; x++){
    if(a[x]){
        r[x] = 1.0;
    }
    else{
        r[x] = 0.0;
    }
}