Worked example
Filtering keeps only the records that meet a condition. Without knowing how many will match, count first, then fill a result array of that exact size:
String[] names = {"Ada", "Sam", "Kai", "Mia"};
int[] scores = {55, 88, 91, 60};
int count = 0;
for (int i = 0; i < scores.length; i++) { // pass 1: how many pass?
if (scores[i] >= 60) {
count++;
}
}
String[] passers = new String[count];
int j = 0;
for (int i = 0; i < scores.length; i++) { // pass 2: collect names
if (scores[i] >= 60) {
passers[j] = names[i];
j++;
}
}
Passing students (score at least 60) are Sam, Kai, and Mia, so passers is {Sam, Kai, Mia}. (An ArrayList from java.util is the other common way to collect matches without two passes.)