【问题标题】:Every String[] within an ArrayList has the same valueArrayList 中的每个 String[] 都具有相同的值
【发布时间】:2015-04-10 15:04:32
【问题描述】:

我正在尝试使用从数据字典中获取的信息来创建用于创建/替换多个视图的 Oracle SQL 脚本。

我已经设法通过 JDBC 驱动程序和对数据字典的查询建立了一个有效的数据库连接,它也可以正常工作并返回正确的值。

但是,在将查询的信息存储在 String 数组中时 - 然后将其添加到 String[] 的 ArrayList 中 - 似乎出现了问题,因为所有数组似乎在它们各自的索引位置具有相同的值,而我没有不知道为什么会这样。

这是我的代码,如果有人能发现错误,我将不胜感激:

public ArrayList<String[]>  getDataDictionary(ArrayList<String> dbInfo, String table) throws SQLException {

ArrayList<String[]> result = new ArrayList<String[]>();
String[] resultTemp = new String[2];

... connection variables (URL, User, Pass)
... get connection, etc.

try {
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME = '" + table + "'");

            while (rs.next()) {
                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");

                // database values
                System.out.println(rs.getString("COLUMN_NAME"));
                System.out.println(rs.getString("DATA_TYPE"));
                // array values
                System.out.println(resultTemp[0]);
                System.out.println(resultTemp[1]);

                //The above sout's return the proper values for each pass of the loop
                //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong

                result.add(resultTemp);
            }

            String[] test = new String[2];

            // sout's return wrong values now, i.e. the value returned is always the same for all arrays queried in the ArrayList
            //I don't understand how that can be, because the correct values were added to the ArrayList a few lines above and now they are wrong with no changes made
            test = result.get(0);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(1);
            System.out.println(test[0]);
            System.out.println(test[1]);

            test = result.get(2);
            System.out.println(test[0]);
            System.out.println(test[1]);

            rs.close();
            statement.close();
            con.close();

            return result;  
} catch(Exception e)
        {
            e.printStackTrace();
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error!");
            alert.setHeaderText("Invalid SQL!");
            alert.setContentText("Please verify the information you provided!");
            alert.showAndWait();
            return null;
        }

【问题讨论】:

    标签: java arrays arraylist


    【解决方案1】:

    您应该在循环内创建数组实例。

           while (rs.next()) {
                String[] resultTemp = new String[2];
                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");
                ....
    

    不这样做会导致同一数组被多次添加到result

    【讨论】:

    • 谢谢,那很快 :) 真是愚蠢的错误,我现在记得了。如果您有一段时间不使用 OOP 语言,就会发生这种情况:/ 总是觉得它有点不直观
    • 与 OOP 无关。在 C 中也会发生同样的情况。
    【解决方案2】:

    您在每个循环中存储对同一对象的引用。

    您必须在每个 lopp 中创建一个新数组:

     while (rs.next()) {
          resultTemp = new String[2];
    
                    resultTemp[0] = rs.getString("COLUMN_NAME");
                    resultTemp[1] = rs.getString("DATA_TYPE");
    
                    // database values
                    System.out.println(rs.getString("COLUMN_NAME"));
                    System.out.println(rs.getString("DATA_TYPE"));
                    // array values
                    System.out.println(resultTemp[0]);
                    System.out.println(resultTemp[1]);
    
                    //The above sout's return the proper values for each pass of the loop
                    //This is what feels the strangest to me. The values are correct here, but when queried later they are wrong
    
                    result.add(resultTemp);
                }
    

    【讨论】:

    • 谢谢,那很快 :) 真是愚蠢的错误,我现在记得了。如果您有一段时间不使用 OOP 语言,就会发生这种情况:/ 总是觉得它有点不直观
    【解决方案3】:

    您正在覆盖同一个数组,String[] resultTemp = new String[2];

    while (rs.next()) {
                String[] resultTemp = new String[2];
                resultTemp[0] = rs.getString("COLUMN_NAME");
                resultTemp[1] = rs.getString("DATA_TYPE");
    

    while 循环内初始化它。这样当你添加

    result.add(resultTemp);
    

    result 将在其中保存resultTemp[] 对象的列表。

    【讨论】:

    • 谢谢,那很快 :) 真是愚蠢的错误,我现在记得了。如果您有一段时间不使用 OOP 语言,就会发生这种情况:/ 总是觉得它有点不直观
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多