-2

我正在为安卓设备制作一个应用程序。我的应用程序中有一个函数,它有 2 个“for 循环”,每个循环 3200 次,访问一个 53 KB 的 .txt 文件(包含 3200 行)并将字符串与每一行进行比较,每次迭代一行。“for 循环”还包含 BufferedReader()、InputStreamReader()、InputStream() 和 StringTokenizer()。因此,当我在模拟器上运行应用程序时,它处理该功能大约需要 8 秒。这是不可接受的。如何将所需的时间减少到半秒或最多。1秒?谢谢!编辑:这是我的程序的一部分,带有 2 个 for 循环:

else if(a==2){
        String z="";
        try{
            InputStream is = getAssets().open("USCOUNTIES.txt");
            InputStreamReader iz=new InputStreamReader(is);
            BufferedReader bis = new BufferedReader(iz);

            int v=0;

            v=count("USCOUNTIES.txt");//counts number of lines in the .txt file
        //finding no. of counties to be displayed
            int counter=0;
            String pos;
            pos=Integer.toString(position);
            try{
            for(int i=0;i<v;i++){
                z=bis.readLine();
                //int x=pos.length();
                boolean a;
                //using stringtokenizer
                StringTokenizer st = new StringTokenizer(z, ","); 
                String substring;
                substring=(String) st.nextElement();
                a=substring.equals(pos);
                if(a==true){

                    counter=counter+1;

                }
            }}catch(Exception e){e.printStackTrace();}
            String array1[]=new String[counter];

            try{
                InputStream ig = getAssets().open("USCOUNTIES.txt");
                InputStreamReader ia=new InputStreamReader(ig);
                BufferedReader bos = new BufferedReader(ia);
            int j=0;
            for(int i=0;i<v;i++){
                z=bos.readLine();
                String[] split = z.split(",");
                if(split[0].equals(pos)){
                    array1[j]=split[1];
                    j=j+1;
                }

            }}
            catch(Exception e){e.printStackTrace();}
4

1 回答 1

1

如果我是你,我只会解析一次所有内容,然后随心所欲地处理它。

这段代码就是这样做的,包括解析Integers(我怀疑你需要这些作为值,而不是作为Strings):

public void read() throws IOException {
    InputStream is = getAssets().open("USCOUNTIES.txt");
    InputStreamReader iz=new InputStreamReader(is);
    BufferedReader bis = new BufferedReader(iz);
    String line = "";
    String firstNumber = "";
    String secondNumber = "";
    String countyName = "";
    StringTokenizer st = null;
    HashMap<Pair, String> map = new HashMap<>();
    while((line = bis.readLine()) != null) {
        st = new StringTokenizer(line, ",");
        firstNumber = (String) st.nextElement();
        st = new StringTokenizer((String)st.nextElement(), ">");
        secondNumber = (String) st.nextElement();
        countyName = ((String) st.nextElement());
        countyName = countyName.substring(0, countyName.length()-1);
        int num1 = Integer.parseInt(firstNumber);
        int num2 = Integer.parseInt(secondNumber);
        map.put(new Pair(num1, num2), countyName);
    }
}

class Pair {
    int num1, num2;
    Pair(int num1, int num2) {
        this.num1 = num1;
        this.num2 = num2;
    }

    public boolean equals(Object other) {
        if (other instanceof Pair) {
            Pair np = (Pair) other;
            return this.num1 == np.num1 && this.num2 == np.num2;
        }
        return false;
    }

    public int hashCode() {
        return (Integer.valueOf(num1).hashCode() >> 13) ^ Integer.valueOf(num2).hashCode();
    };
}

现在您可以使用以下行简单地检索每个countyName

String s = map.get(new Pair(1,69));

这又回来了Aleutians East

我希望这能让你开始。

编辑

这段代码使用 2D SparseArray(很像HashMap<Integer, Object>)。这样,所有内容都按第一个数字排序。

public class Reader {
    private String firstNumber = "";
    private String secondNumber = "";
    private String countyName = "";
    private StringTokenizer stringTokenizer = null;
    private SparseArray<SparseArray<String>> sparseArray = new SparseArray<SparseArray<String>>();
    private SparseArray<String> temporarySparseArray = null;

    public void readFromIS() throws IOException {
        InputStream is = getAssets().open("USCOUNTIES.txt");
        InputStreamReader iz=new InputStreamReader(is);
        BufferedReader bis = new BufferedReader(iz);
        String line = null;
        while((line = bis.readLine()) != null) {
            readLine(line);
        }
    }

    public void readFromList() {
        String[] strings = {
                "0,1>Autauga;",
                "0,2>Baldwin;",
                "0,3>Barbour;",
                "1,69>Aleutians East;",     
                "1,68>Aleutians West;"
        };
        for (String line : strings) {
            readLine(line);
        }
    }

    private void readLine(String line) {
        stringTokenizer = new StringTokenizer(line, ",");
        firstNumber = (String) stringTokenizer.nextElement();
        stringTokenizer = new StringTokenizer((String)stringTokenizer.nextElement(), ">");
        secondNumber = (String) stringTokenizer.nextElement();
        countyName = ((String) stringTokenizer.nextElement());
        countyName = countyName.substring(0, countyName.length()-1);
        int num1 = Integer.parseInt(firstNumber);
        int num2 = Integer.parseInt(secondNumber);
        if (sparseArray.get(num1) == null) {
            sparseArray.put(num1, new SparseArray<String>());
        }
        temporarySparseArray = sparseArray.get(num1);
        temporarySparseArray.put(num2, countyName);
        sparseArray.put(num1, temporarySparseArray);
        temporarySparseArray = null;
    }

    public void test() {
        readFromList();
        String s = sparseArray.get(0).get(2);
        SparseArray sa = sparseArray.get(0);
        System.out.println(sa.size()); //should be 3
        System.out.println(s); // should be Baldwin
    }
}

要检索所有以 0 开头的县num1,您只需使用:

SparseArray<String> startingWithZero = sparseArray.get(0);

仅供参考: ASparseArrayHashMapfor integers,因此并非所有内容都必须自动装箱(从Integerto int,因为您不能将原始类型放入 a HashMap)。

EDIT2你打印一维稀疏数组的地址。

public void printEverythingStartingWithZero() {
    SparseArray<String> subSparseArray = sparseArray.get(0); //You first need a 1D sparseArray
    int key = 0;
    for(int i = 0; i < subSparseArray.size(); i++) {
       key = subSparseArray.keyAt(i);
       String county = subSparseArray.get(key); //county is the String in place (0,key)
       System.out.println(county);
    }
}

您需要先检索一维数据sparseArray,前导零。

于 2013-03-16T22:57:34.003 回答