Java code for collectively filter 0 and 1 of Array a={0100110101}
This is simple core java code for separating a common array. I am taking an array.
Iterating it gets the desired output.
import java.util.List;
import java.util.ArrayList;
public class FilterArray {
public static void main(String[] args) {
int[] a = {0, 1, 0, 0, 1, 1, 0, 1, 0, 1};
List<Integer> zeros = new ArrayList<>();
List<Integer> ones = new ArrayList<>();
for (int i : a) {
if (i == 0) {
zeros.add(i);
} else {
ones.add(i);
}
}
System.out.println("Zeros: " + zeros);
System.out.println("Ones: " + ones);
}
}
The program creates two ArrayLists: zeros to store the 0s, and ones to store the 1s. It then loops through the array a and adds the elements to the appropriate list based on whether the element is 0 or 1. Finally, the program prints out the two lists.
Programm output
Zeros: [0, 0, 0, 0, 0]
Ones: [1, 1, 1, 1, 1]

No comments:
Add your comment