Java,字符串数组(字符串)数组的问题(可能是动态的)

时间:2013-10-02 16:16:36

标签: java string dynamic-arrays

为了将查找搜索加速到多记录文件中,我希望将其元素存储到数组的String数组中,以便我只能将类似“AF”的字符串搜索到类似的字符串中(“AA”,“ AB,...,“AZ”)而不是整个文件。

原始文件是这样的:

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

我想翻译成

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

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

我尝试了解决方案:

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

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分开。 : - )

1 个答案:

答案 0 :(得分: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
        }