Quickest way of storing and accessing Strings in an array

54 views Asked by At

I know I could go through a for loop like this (see code) and i could also add to the array in the same fashion but is there a quicker way. I don't want to use any other java API as i want to practice array's. Would using a hash function allow me to store my variables quicker and then find a certain word quicker?

edit: The problem is when using 10,000+ words the delay increases larger than 1ms

Thanks :)

int count = 0;
for(int i = 0; i < array.length; i++)
    if(array[i].equals(word)) 
        count++;
3

There are 3 answers

0
JGerulskis On

Whenever your processing 10,000 words you can expect some delays. Depending on what word is set to you might be able to filter a bit better but as far as your code shows that's the best way to do it.

9
Sarthak Mittal On

You can use a 2 dimensional array :

Array [Alphabet] [Words starting with that alphabet]

0
OldCurmudgeon On

You could pre-sort the array and then use a binary-chop search on it. This would only be useful if you are looking for many words.

If you allow other structures then you can usually achieve O(1) lookup times.