0

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

4
  • so what do you have problems with? Commented Jul 3, 2014 at 12:39
  • changing a final object is not possible Commented Jul 3, 2014 at 12:40
  • @Opiatefuchs That wasn't supposed to be there, i've removed it from my question now. Commented Jul 3, 2014 at 12:41
  • 2
    ok, then follow nr4bt´s answer....thats correct Commented Jul 3, 2014 at 12:45

2 Answers 2

3

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

Comments

0

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;

}

}

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.