You can order the names, use binary search with a case-insensitive comparator to find the insertion spot for the potential prefix, and walk the array to catch all other words with the same prefix:
// At preparation time
Arrays.sort(names, String.CASE_INSENSITIVE_ORDER);
...
// At query time
int pos = Arrays.binarySearch(names, query, String.CASE_INSENSITIVE_ORDER);
if (pos < 0) {
pos = -(pos+1);
}
while (pos < names.length) {
if (names[pos].toLowerCase().startsWith(query.toLowerCase())) {
c.addRow(new Object[]{pos, names[pos]});
pos++;
} else {
break;
}
}
Arrays.binarySearch
finds an insertion point. If the name matches, pos
would be non-negative; otherwise, you need to convert it to a valid index with this expression: -(pos+1)
If query
is a proper prefix, its insertion point would be in front of the first name with a matching prefix. Since names
is sorted, all entries with the same prefix would be next to each other. That's why you can walk the list linearly until first mismatch, and stop at that point.