Answer the question
In order to leave comments, you need to log in
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) {
}
}
}
}
Answer the question
In order to leave comments, you need to log in
if(arr1[i] == 1) {
arr1[i] = 0
} else {
arr1[i] =1
}
UPD even better
and it is possible without the condition
arr1[i]=(arr[i]+1)%2
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question