简体   繁体   English

在数组中存储和访问字符串的最快方法

[英]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. 我知道我可以经历这样的for循环(请参见代码),并且我也可以以相同的方式添加到数组中,但是有一种更快的方法。 I don't want to use any other java API as i want to practice array's. 我不想使用任何其他Java API,因为我想练习数组。 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 编辑:问题是当使用10,000+个单词时,延迟增加大于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. 如果允许其他结构,则通常可以实现O(1)查找时间。

Whenever your processing 10,000 words you can expect some delays. 每当处理10,000个字时,您都可能会遇到一些延迟。 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. 根据设置的单词,您可能可以进行更好的过滤,但是就您的代码而言,这是最好的过滤方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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