Reputation: 56
I have a problem with fast read txt file to ArrayList. If I want read a file size 0,9MB, I must wait 5min. If files size is 34MB (partialy, because android does not accept larger than 1MB files), it's completely not working. I think that process should be max few second.
This is a code:
String word;
public ArrayList<String> dictionary = new ArrayList<String>();
public void setup()
{
try {
AssetManager assetManager = getAssets();
InputStream inputf;
inputf = assetManager.open("dict_1.txt");
reader = new BufferedReader(new InputStreamReader(inputf));
word = " ";
while(word != null)
{
word = reader.readLine();
if (word != null)
dictionary.add(word);
}
if(reader.equals("null")) println("No file found");
} catch (NullPointerException e) {
e.printStackTrace();
println("No file found");
} catch (IOException e) {
e.printStackTrace();
}
}
I'm sorry for my english. I hope that all is understadable.
Upvotes: 3
Views: 819
Reputation: 2535
Your problem is adding word to arraylist In arraylist
read operations are constant time - O(1) but write
operations have the potential to run out of space in the backing array
, re-allocation
, and a copy
- so that runs in O(n) time.so you should use linkedlist
instead of arraylist
.It provide efficiency o(1)
(for adding)
LinkedList<String> dictionary = new LinkedList<String>();
Upvotes: 1
Reputation: 41347
Your ArrayList
keeps getting reallocated as you're adding items. This can consume a non-trivial amount of CPU time, especially as the list grows, since a bunch of pointers need to copied around in memory. A better approach would be to store the number of entries as the first item of the dictionary file, and then pre-allocate the ArrayList
:
dictionary = new ArrayList<String>(numberOfEntries);
A more advanced optimization would be a data structure that doesn't rely on a Java collection class at all. Depending on your needs, this could be a huge UTF-8 byte array that is read into memory in one swoop (or even accessed via a memory-mapped file).
Upvotes: 3