简体   繁体   中英

Strange behaviour of ArrayList

I am writing a website using JSP, JSTL, Servlets and JavaBeans.

At one point of my code, I am trying to use an ArrayList of objects, and a strange thing is happening: when I add the first object it is fine, and when I add a second object it adds it in the second place, but the object at index(0) gets the same values as the object at index(1).

Maybe a problem is in the

ArrayList<Article> articleList = new ArrayList<Article>();
Article newArticle = new Article();

Since articleList is ArrayList of Article class.

Can somebody point me to what I am doing wrong?

Below is my code:

public ArrayList<Article> getArticles()
{
    baseIO mySql = new baseIO();
    ArrayList<Article> articleList = new ArrayList<Article>();
    int articleId = 0;

    try
    {
        String sql =
            "select * from jsp_blog_article order by article_id Desc Limit 3";
        con = (Connection)mySql.getConnection();
        pstmt = (PreparedStatement) con.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Article newArticle = new Article();
            newArticle.setArticleAuthor(rs.getString("article_name"));
            newArticle.setArticleBody(rs.getString("article_body"));
            newArticle.setArticleAuthor(rs.getString("article_author"));
            newArticle.setArticleDate(rs.getString("article_date"));
            articleId = Integer.parseInt(rs.getString("article_id"));
            newArticle.setArticleId(String.valueOf(articleId));
            newArticle.setArticleComments(this.getCommentsNum(articleId));
            articleList.add(newArticle);
        }
        con.close();
        pstmt.close();
    }
    catch(Exception e)
    {
        return null;
    }

    return articleList;
}

And the Article class

package objects;

import java.io.Serializable;

public class Article implements Serializable{
    private String articleName;
    private String articleBody;
    private String articleAuthor;
    private String articleComments;
    private String articleDate;
    private String articleId;

    public Article()
    {

    }

    // all the getters and setters in place, but it is too long
    // so i am not going to post them in forum

}

您两次调用newArticle.setArticleAuthor ...我知道这不是列表问题的一部分,但这是一个观察。

I would try it this way and see what this does.

int x = 0;
while (rs.next()) {
    articleList.add(new Article());
    articleList.get(x).setArticleName(rs.getString("article_name"));
    articleList.get(x).setArticleBody(rs.getString("article_body"));
    articleList.get(x).setArticleAuthor(rs.getString("article_author"));
    articleList.get(x).setArticleDate(rs.getString("article_date"));
    articleList.get(x).setArticleId(rs.getString("article_id"));
    articleList.get(x).setArticleComments(this.getCommentsNum(articleId));
    x++;
}

The code should be cleaned up per the other comments, but functionally, it should work.

Here's what I think is happening.

Your code has the following two lines in it:

newArticle.setArticleAuthor(rs.getString("article_name"));
newArticle.setArticleAuthor(rs.getString("article_author"));

and there is no corresponding call to:

newArticle.setArticleName(rs.getString("article_name"));

this means that your object has no article name specified (even though the author is specified). I'll bet that you are then doing some sort of processing before you display the list that somehow merges articles with the same name.

As a general approach to debugging, I recommend that you mock up your code so you can run it in a debugger and see what's actually going on (right now, your system has so many moving parts that it's going to be difficult for you to hone in on the actual problem).

In the current case, this would be as simple as running the one method outside of your web container, and using a debugger to take a look at the objects in the list that gets returned. You will find that the objects in the list are, indeed, separate objects - just having the same articleName property.

Code looks fine, how are you displaying the list that makes you think the same value is at both indexes? Perhaps your problem is with that code.

Are you adding articles to the database concurrently while reading them? I think that it is possible, depending on your storage engine, that you'd have problems reading while updating is going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM