I need to write a method to return the index array of a character in a string in Java. Is the following good (correctness, efficiency, as short code as possible) enough?
int[] charIndexArray(String s, char c) {
int start = 0;
List<Integer> list = new ArrayList<Integer>();
while ((start = s.indexOf(c, start)) != -1) {
list.add(start);
start++;
}
int arr[] = new int[list.size()];
for (int i = 0; i < ret.length; i++)
arr[i] = list.get(i);
return arr;
}