You may use a container such as a List to store each valid index, also careful the stop condition should be i < hair.length
String[] hair = {"short", "long", "long"};
List<Integer> result = new ArrayList<>();
for (int i = 0; i < hair.length; i++) {
if (hair[i].equalsIgnoreCase("long")) {
result.add(i);
}
}
System.out.println(result); // [1, 2]
Use a int[] to store is a bit different, as you'll have less valid index as the size of the array, ending with empty box at the end (0 value), you'll need a Arrays.copyof to truncate it
String[] hair = {"short", "long", "long", "short", "long", "long", "short", "long", "long"};
int[] indices = new int[hair.length];
int nb_valid = 0;
for (int i = 0; i < hair.length; i++) {
if (hair[i].equalsIgnoreCase("long")) {
indices[nb_valid++] = i;
}
}
System.out.println(Arrays.toString(indices)); // [1, 2, 4, 5, 7, 8, 0, 0, 0]
indices = Arrays.copyOf(indices, nb_valid); // truncate the array to keep real indices only
System.out.println(Arrays.toString(indices)); // [1, 2, 4, 5, 7, 8]