我用arraylist.add()添加一个元素后,再添加一个,get(0)和get(1)都得到的是get(1)的值
为什么啊??????

解决方案 »

  1.   

    添加的元素值不是同一个吧?arraylist.add("00")
    arraylist.add("11")这样测试一下
      

  2.   

    public class BookDisplay {
    int maxRows=20;
    int rows;
    int pages;
    int currentPage=1;
    String condition="";
    ArrayList list=new ArrayList();
    Book book=new Book();

    public BookDisplay() {
    }

    public BookDisplay(int maxRows,String condition) {
    this.maxRows=maxRows;
    this.condition=condition;
    }

    public ArrayList List(){
    DBAccess db=new DBAccess();
    db.createConn();
    String sql=null;
    int temp=0; if((currentPage-1)!=0){
    sql="set rowcount "+ maxRows*(currentPage-1) +" select book_id from book_info" +condition+" order by book_id set rowcount 0";
    System.out.println(sql);
    db.query(sql);
    while(db.next()){
    temp=Integer.parseInt(db.getValue("book_id"));
    }
    db.closeRs();
    db.closeStm();
    } String conditionTemp=condition.replaceFirst("where", "and");
    sql="set rowcount " + maxRows + " select book_info.*,book_sort.sort_name from book_info,book_sort where book_info.sort_id=book_sort.sort_id and book_id>"+temp +conditionTemp+" order by book_id set rowcount 0";
    db.query(sql);
    int i=0;
    //System.out.println(sql);
    while(db.next()) {
    book.setBookId(Integer.parseInt(db.getValue("book_id")));
    book.setAuthor(db.getValue("author"));
    book.setBookSerial(db.getValue("book_serial"));
    book.setBookName(db.getValue("book_name"));
    book.setChargeFlag(db.getValue("charge_flag"));
    book.setISBN(db.getValue("ISBN"));
    book.setLogoutFlag(db.getValue("logout_flag"));
    book.setPress(db.getValue("press"));
    //book.setPrice(Float.parseFloat(db.getValue("price")));
    book.setSortName(db.getValue("sort_name"));
    book.setTranslator(db.getValue("translator"));
    list.add(book);
    // System.out.println("book:"+list.get(0).getBookId());
    // System.out.println("serial:"+list.get(0).getBookSerial());

    i++;
    // list.add(i);
    // System.out.println("i="+i);

    }

    // System.out.println("serial:"+list.get(0).getBookSerial());××××××××××××××××××这里打印出来就是第二个元素的值了
    db.closeRs();
    db.closeStm();
    db.closeConn();
    return list;
    }
      

  3.   

    while(db.next()) {
    Book book=new Book();
    .....
      

  4.   

    while(db.next()&&db!=null){
    Book book=new Book(); ...
      

  5.   


    就是这样,你在外面新建的Book,在list里放的可不是就一个呗~你得每循环一次新建一个
      

  6.   

    BOOK放到while(db.next()) {这句后面来NEW吧!
      

  7.   

    BOOK 是在while外面new的,所以每次都是用的同一个bean,BOOK放到while(db.next()) 里面就可以了
      

  8.   

    楼上的各位说的都是正解,你的list中加的都是同一个book对象,只是你在循环时把book的属性值改了
    应该把
    Book book=new Book(); 放到循环里,这样list中加入的才会是不同的book对象
      

  9.   

    list.add(book); 很明显,你只添加了一个book,你的new Book 只有一个。
    应该在循环开始的地方,每次都new一个新的才行
      

  10.   

    为什么重新赋值之后List中前面的元素还会改变呢
      

  11.   

    while(db.next()) {
    Book book=new Book();//创建对象放这里,不然 你添加N个 它的值都是一样的
    book.setBookId(Integer.parseInt(db.getValue("book_id")));
    book.setAuthor(db.getValue("author"));
    book.setBookSerial(db.getValue("book_serial"));
    book.setBookName(db.getValue("book_name"));
    book.setChargeFlag(db.getValue("charge_flag"));
    book.setISBN(db.getValue("ISBN"));
    book.setLogoutFlag(db.getValue("logout_flag"));
    book.setPress(db.getValue("press"));
    //book.setPrice(Float.parseFloat(db.getValue("price")));
    book.setSortName(db.getValue("sort_name"));
    book.setTranslator(db.getValue("translator"));
    list.add(book);
    // System.out.println("book:"+list.get(0).getBookId());
    // System.out.println("serial:"+list.get(0).getBookSerial());i++;
    // list.add(i);
    // System.out.println("i="+i);}