简体   繁体   中英

Quickest way of storing and accessing Strings in an array

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++;

You can use a 2 dimensional array :

Array [Alphabet] [Words starting with that alphabet]

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM