Edit: The question presents a source code fragment based on array indices, hence my original answer:
Use the Arrays class to do this for you.
String[] names = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
String[] truncated = java.util.Arrays.copyOf(names, names.length-4); // remove the last 4 names
System.out.println(java.util.Arrays.toString(truncated));
Try it online here.
Edit: Since people (not the OP) weren't too happy with that, I added: Or, to match only names of the form Firstname Lastname
, use a regex:
String[] input = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
List<String> namesList = new ArrayList<>();
for(String name : input) {
if(name != null && name.matches("^[A-Z][A-z]+ [A-Z][a-z]+$"))
namesList.add(name);
}
String[] namesArray = namesList.toArray(new String[0]);
System.out.println(Arrays.toString(namesArray));
Try it online here.
Edit: Finally, since Dukeling commented on fim
meaning end
in Portuguese, a better solution might be:
Use a loop to find the first occurrence of fim
and then truncate the array accordingly (as in the first code snippet in my answer).
String[] names = new String[] {"Maria Carolina", "Luisa Joana", "Lara Silva", "Catarina Patricio", "Paula Castro", "fim", null, null, null};
int newLength = names.length;
for(int i = 0; i < names.length; i++) {
if("fim".equals(names[i])) {
newLength = i;
break;
}
}
String[] truncated = java.util.Arrays.copyOf(names, newLength);
System.out.println(java.util.Arrays.toString(truncated));
Try it online here.