0

为了加快对多记录文件的查找搜索,我希望将其元素存储到数组的字符串数组中,这样我就可以仅将像“AF”这样的字符串搜索到类似的字符串中(“AA”、“AB”、. .. , "AZ") 而不是整个文件。

原始文件是这样的:

AA
ABC
AF
(...)
AP
BE
BEND
(...)
BZ
(...)
SHORT
VERYLONGRECORD
ZX

我想翻译成

AA  ABC     AF  (...)   AP
BE  BEND    (...)   BZ
(...)
SHORT
VERYLONGRECORD
ZX

我不知道有多少记录以及每个“行”将有多少“元素”,因为源文件可以随时间改变(即使在被读入内存后,数组只是被读取)。

我尝试了 whis 解决方案:

在一个类中,我定义了(字符串)数组的字符串数组,但没有定义它的维度

public static String[][] tldTabData;

然后,在另一堂课上,我阅读了文件:

public static void tldLoadTable() {

    String rec = null;
    int previdx = 0;
    int rowidx = 0;
    // this will hold each row
    ArrayList<String> mVector = new ArrayList<String>();

    FileInputStream fStream;
    BufferedReader bufRead = null;

    try {
        fStream = new FileInputStream(eVal.appPath+eVal.tldTabDataFilename);
        // Use DataInputStream to read binary NOT text.
        bufRead = new BufferedReader(new InputStreamReader(fStream));
    } catch (Exception er1) {
        /* if we fail the 1.st try maybe we're working into some "package" (e.g. debugging)
         * so we'll try a second time with a modified path (e.g. adding "bin\") instead of
         * raising an error and exiting.
         */
        try {
            fStream = new FileInputStream(eVal.appPath +
                "bin"+ File.separatorChar + eVal.tldTabDataFilename);
            // Use DataInputStream to read binary NOT text.
            bufRead = new BufferedReader(new InputStreamReader(fStream));
        } catch (FileNotFoundException er2) {
            System.err.println("Error: " + er2.getMessage());
            er2.printStackTrace();
            System.exit(1);
        }
    }
    try {
        while((rec = bufRead.readLine()) != null) {
            // strip comments and short (empty) rows
            if(!rec.startsWith("#") && rec.length() > 1) {
                // work with uppercase only (maybe unuseful)
                //rec.toUpperCase();
                // use the 1st char as a row index
                rowidx = rec.charAt(0);
                // if row changes (e.g. A->B and is not the 1.st line we read)
                if(previdx != rowidx && previdx != 0)
                {
                    // store the (completed) collection into the Array
                    eVal.tldTabData[previdx] = mVector.toArray(new String[mVector.size()]);
                    // clear the collection itself
                    mVector.clear();
                    // and restart to fill it from scratch
                    mVector.add(rec);
                } else
                {
                    // continue filling the collection
                    mVector.add(rec);
                }
                // and sync the indexes
                previdx = rowidx;
            }
        }
        streamIn.close();
        // globally flag the table as loaded
        eVal.tldTabLoaded = true;
    } catch (Exception er2) {
        System.err.println("Error: " + er2.getMessage());
        er2.printStackTrace();
        System.exit(1);
    }
}

执行程序时,它会正确地将字符串累积到 mVector 中,但是当尝试将它们复制到 eVal.tldTabData 中时,我得到了 NullPointerException。

我敢打赌,我必须在某个时候创建​​/初始化数组,但是在确定位置和方式时遇到了问题。

我第一次在 Java 中编码... helloworld 分开。:-)

4

1 回答 1

0

您可以使用 Map 来存储每行的字符串;

这里有一些你需要的东西:

        //Assuming that mVector already holds all you input strings
        Map<String,List<String>> map = new HashMap<String,List<String>>();

        for (String str : mVector){
            List<String> storedList;
            if (map.containsKey(str.substring(0, 1))){
                storedList = map.get(str.substring(0, 1));
            }else{
                storedList = new ArrayList<String>();
                map.put(str.substring(0, 1), storedList);
            }
            storedList.add(str);
        }

        Set<String> unOrdered = map.keySet();
        List<String> orderedIndexes = new ArrayList<String>(unOrdered);
        Collections.sort(orderedIndexes);

        for (String key : orderedIndexes){//get strings for every row
            List<String> values = map.get(key);
            for (String value : values){//writing strings on the same row
                System.out.print(value + "\t"); // change this to writing to some file
            }
            System.out.println(); // add new line at the end of the row
        }
于 2013-10-02T16:36:08.957 回答