how to faster read text file to ArrayList on android

你离开我真会死。 提交于 2019-12-31 02:09:08

问题


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.


回答1:


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).




回答2:


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>();


来源:https://stackoverflow.com/questions/21665733/how-to-faster-read-text-file-to-arraylist-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!