在java中启动对象的位置

时间:2012-06-28 10:18:05

标签: java while-loop

这个问题与我对另一个问题的回答有关。 最初的问题是here

任何人都可以解释为什么坏代码会以评论中解释的方式失败(因为它是伪代码)

 // bad code
        ResultSet rs = getDataFromDBMS("Select * from [tableName];");

            //temp uses a collection member within it to hold a list of column names to data value pairs (hashmap<String,String>)
        Object temp = new objectToHoldInfoFromResultSet();

        // loop over the result set
        while (rs.next)// for each row in the result set
        {
            for (int i = 1; i <= rs.getNumberColums; i++) {
                temp.add(infoAboutColumn);
            }
            temp.printInfo();// always prints correct (ie current) info
                    //the print just takes the hashmap member and asks for a       
                    //toString() on it
            anotherObject(makeUseOf(temp));// always uses info from first
                   //iteration. Essentially grabs the hashMap member of temp, and puts
                   //this into another collection of type 
                   //HashMap< HashMap<String,String>, temp> see the linked question
                   //for more detail.

        }

        // Seemingly each loop into the while the temp.doSomethingToData(); uses
        // the temp object created in the first iteration

        // good code
        ResultSet rs = getDataFromDBMS("Select * from [tableName];");

        // loop over the result set
        while (rs.next)// for each row in the result set
        {
            Object temp = new objectToHoldInfoFromResultSet();// moving
                                                                // declaration
                                                                // of temp into
                                                                // the while
                                                                // loop solves
                                                                // the problem.
            for (int i = 1; i <= rs.getNumberColums; i++) {
                temp.add(infoAboutColumn);
            }
            temp.printInfo();// always prints correct (ie current) info

            anotherObject(makeUseOf(temp));// also uses the correct (ie current)
                                            // info.

        }

2 个答案:

答案 0 :(得分:1)

我不确定为什么一个好,另一个坏。我的猜测(不知道objectToHoldInfoFromResultSet和其他方法行为是什么)如下:

在第一个实例中,“objectToHoldInfoFromResultSet”(应该大写)每次创建一次

temp.add(infoAboutColumn);
调用

,将新记录数据添加到对象。我猜这个信息应该为每条记录清除......否则你会得到很多重复。通过重新初始化持有者对象来处理重复。即(1 2 3 4 5 6)而不是(1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6)。

如果不了解更多关于各种礼仪的话,我可以告诉你更多。

答案 1 :(得分:1)

如果不知道temp.printInfo()makeUseOf()正在做什么,我们无法可靠地回答这个问题。实现它们很容易按照你描述的方式运行。

当您在循环外实例化temp时,您将在循环的所有迭代中使用相同的对象。因此,它可以从每次迭代中收集数据(例如,收集到集合中)。然后,方法可以在当前迭代中以及从之前的任何迭代中获取数据,如果不是这样的话,可能会导致问题。

因此,我们假设temp包含一个集合,并在每次迭代中将结果集中的一列添加到其中。现在,如果实现了temp.printInfo()来打印有关此集合中 last 元素的信息,则实现makeUseOf()以对 first 元素执行某些操作集合,你得到你观察到的行为。

OTOH当你在循环中实例化temp时,你会在每次迭代中得到一个新的,不同的对象,它不会“记住”早期迭代中的任何数据。因此,即使上面列出了temp.printInfo()makeUseOf()的实现,您也会得到正确的结果。