简体   繁体   中英

Changing values of boolean Array programmatically

I would like to know if there would be a possible way to change the values of the boolean array I have set up:

boolean[] checked = new boolean[]{true, false, true, false, true};

Would I be able to programmatically change these values individually? I know you can do Arrays.fill(array, true); to fill all of them, but what about individually? thanks

You can change the value simply by accessing array element via index. An array is a java object, thus by declaring final, you can not assign a new reference but values still can be changed.

final boolean[] checked = new boolean[]{true, false, true, false, true};

System.out.println(checked[0]);   //output : true

checked[0] = false;

System.out.println(checked[0]);   //output : false

try this

public class Boolean {


public static void main(String[] args) {

    final boolean[] checked = new boolean[]{true, false, true, false, true};

    for(int i=0; i<checked.length;i++){

        if(checked[i]==true){
            checked[i]=false;
            System.out.println(String.valueOf(checked[i]));

        }else{
            checked[i]=true;
            System.out.println(String.valueOf(checked[i]));
        }
    }

    return;

}

}

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