ArrayList表现得非常奇怪

时间:2012-03-13 08:54:53

标签: java jdbc arraylist

我正在使用 jdbc sql XLS 表中检索数据。以下是我的计划。

案例1:

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
c = DriverManager.getConnection( "jdbc:odbc:Excel Files", "", "" );
stmnt = c.createStatement();
String query = "select * from [Proficiency$];";
ResultSet rs = stmnt.executeQuery( query );
baseColumnList = new ArrayList();
while( rs.next()) {
    //baseColumnList.add(rs.getString(1));
    System.out.println(rs.getString(1));
    //System.out.println(baseColumnList);
}

输出:

Aadmin
Badmin
Badminimage
batch
Cadmin
Dadmin
Eadmin
rulesengine
Fadmin
genadmin
Gadmin
Hdomain-EU
Padmin
IAA

案例2:

Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
c = DriverManager.getConnection( "jdbc:odbc:Excel Files", "", "" );
stmnt = c.createStatement();
String query = "select * from [Proficiency$];";
ResultSet rs = stmnt.executeQuery( query );
baseColumnList = new ArrayList();
while( rs.next()) {
    baseColumnList.add(rs.getString(1));
    System.out.println(baseColumnList);
}

输出:

[Aadmin]
[Aadmin, Badmin]
[Aadmin, Badmin, Badminimage]
[Aadmin, Badmin, Badminimage, batch]
[Aadmin, Badmin, Badminimage, batch, Cadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin, genadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin, genadmin, Gadmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin, genadmin, Gadmin, Hadmin-EU]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin, genadmin, Gadmin, Hadmin-EU, Padmin]
[Aadmin, Badmin, Badminimage, batch, Cadmin, Dadmin, Eadmin, rulesengine, Fadmin, genadmin, Gadmin, Hadmin-EU, Padmin, IAA]

请有人解释为什么arraylist表现得很苛刻。

更新:首先输出预期结果。

4 个答案:

答案 0 :(得分:2)

System.out.println(baseColumnList);

打印列表的实际内容 1 。结果并不奇怪。每次迭代时列表都会变大。


重复第一个例子的输出:

while( rs.next()) {
  baseColumnList.add(rs.getString(1));
  System.out.println(baseColumnList.get(baseColumnList.size()-1));
}

但那是胡说八道。以下代码段更优雅:

while( rs.next()) {
  String columnValue = rs.getString(1);
  baseColumnList.add(columnValue);
  System.out.println(columnValue);
}

1 实际上会调用baseColumnList.toString()

答案 1 :(得分:1)

我不太确定你认为'奇怪'。 在第二种情况下,您将为结果集中的每个结果打印整个baseColumnList。

System.out.println(baseColumnList); 

正如你所看到的那样,它会不断地将这些项目添加到你的baseColumnlist中,所以这一点都不奇怪,事实上它就是你期望发生的事情

祝你好运!

答案 2 :(得分:0)

这似乎是您的代码所期望的。让我解释一下:

//this line adds a value to the list
baseColumnList.add(rs.getString(1));

//this line prints the whole list
System.out.println(baseColumnList);
  • 在第一行中,您将值添加到列表
  • 在第二行中打印整个列表。

在每次迭代中,列表增长一个元素并在其后打印。为防止这种情况,请不要打印整个列表,而是打印一些中间值,例如:

//intermediate value
String temp= rs.getString(1);
//add it to the list
baseColumnList.add(temp);
//print only the value, and not the whole list
System.out.println(temp);

答案 3 :(得分:0)

    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );

        c = DriverManager.getConnection( "jdbc:odbc:Excel Files", "", "" );
        stmnt = c.createStatement();
        String query = "select * from [Proficiency$];";
        ResultSet rs = stmnt.executeQuery( query );
        baseColumnList = new ArrayList();
        while( rs.next())
        {

            baseColumnList.add(rs.getString(1));
        }
       for(Object o: baseColumnList)
       {
         System.out.println((String)o);

       }