J
J
Janna16032020-06-17 14:50:50
Java
Janna1603, 2020-06-17 14:50:50

How to write programs with arrays?

Specify an integer array consisting of elements 0 and 1. For example: [ 1, 1, 0, 0, 1, 0, 1, 1, 0, 0 ]. With the help of a loop and a condition, replace 0 with 1, 1 with 0;

public class Main {

    public static void main(String[] args) {
  int[] arr1 = new int[] {1, 1, 0, 0, 1, 0, 1, 1, 0, 0};
    for (int i = 0; i < arr1.length; i++) {
        if (arr1[i] == 1) {
        }
    }
}
}

So I started, but I don’t know how.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
ruslan_shv, 2020-06-18
@ruslan_shv

what don't you like
arr1[i] ^= 1;
?

D
DDwrt100, 2020-06-17
@DDwrt100

if(arr1[i] == 1) {
arr1[i] = 0
} else {
arr1[i] =1
}
UPD even better

S
Sergei Chamkin, 2020-06-17
@Sergei1337

and it is possible without the condition
arr1[i]=(arr[i]+1)%2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question