【问题标题】:Adding different string arrays into List<String[]>, but returning the same value将不同的字符串数组添加到 List<String[]>,但返回相同的值
【发布时间】:2016-02-21 10:59:11
【问题描述】:

我正在将一个数据库文件写入bus_stopdb.properties 文件。它看起来像这样:(这是文件的一部分)

2X-bound1-stop0-stopcode=CH42W09500
2B-bound1-stop2-stopseq=2
11C-bound2-stop14-stopname=\u725B\u982D\u89D2\u9435\u8DEF\u7AD9
11D-bounds=2
13D-bound2-stop31-stopcode=SA14S32000
11D-bound1-stop17-stopname=\u89C0\u5858\u78BC\u982D
11D-bound2-stop9-stopcode=KW16W22500
2A-bound1-stop29-stopcode=ME01T11000
14D-bound1-stop18-stopcode=LE01W13000
11X-bound1-stop12-stopseq=12
16-bound1-stop3-stopseq=3
23M-bound1-stop12-stopseq=12

我要将属性文件的内容加载到List&lt;String[]&gt;。但我发出了一个问题,即List 的内容都是same。这是一个加载文件的函数:

public static boolean loadDatabase(boolean fromClassResources){
        try {
            File file;
            Properties prop = new Properties();
            InputStream in;
            if (fromClassResources){
                in = KmbApi.class.getClassLoader().getResourceAsStream("bus_stopdb.properties");
            } else
            {
                file = new File("bus_stopdb.properties");
                if(!file.exists()){
                        return false;
                }
                in = new FileInputStream(file);
            }
            prop.load(in);
            int buses = Integer.parseInt(prop.getProperty("buses"));
            int bounds;
            int stops;
            String[] data = new String[5];
            for (int i = 0; i < buses; i++){
                data[0] = bus_db[i];
                bounds = Integer.parseInt(prop.getProperty(bus_db[i] + "-bounds"));
                for (int j = 1; j <= bounds; j++){
                    System.out.println("Bus: " + bus_db[i] + " Bound: " + j);
                    try {
                        stops = Integer.parseInt(prop.getProperty(bus_db[i] + "-bound" + j + "-stops"));
                    } catch (NullPointerException e){
                        continue;
                    }
                    data[1] = Integer.toString(j);
                    for (int s = 0; s < stops; s++){
                        data[2] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopcode");
                        data[3] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopseq");
                        data[4] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopname");
                        //Printing the building array
                        System.out.println(Arrays.deepToString(data));
                        busstop_pair.add(data);
                    }
                }
            }
            //Printing the arrays in the List<String[]>
            System.out.println(Arrays.deepToString(busstop_pair.toArray()));
            in.close();
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

第一个println 给了我一个正确的结果(不同的结果)。 但是第二个println给了我一个无效的结果,它们都是一样的!

我无法弄清楚代码发生了什么。

【问题讨论】:

    标签: java arrays list arraylist


    【解决方案1】:

    您要多次将同一个数组添加到列表中。您必须为循环的每次迭代创建一个新数组,以便在 List 中有不同的元素:

                    for (int s = 0; s < stops; s++){
                        data = new String[5];
                        data[0] = bus_db[i];
                        data[1] = Integer.toString(j);
                        data[2] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopcode");
                        data[3] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopseq");
                        data[4] = prop.getProperty(bus_db[i] + "-bound" + j + "-stop" + s + "-stopname");
                        //Printing the building array
                        System.out.println(Arrays.deepToString(data));
                        busstop_pair.add(data);
                    }
    

    【讨论】:

    • 哦,谢谢。我现在发现了问题。它工作得很好。很抱歉堆栈溢出需要我等待 8 分钟才能接受答案。
    猜你喜欢
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多