2012-11-11 87 views
0

我在通過其ID刪除數據時遇到問題。 我在我的數據列表中有一本書,我想根據其ID刪除特定的書籍。 我試過下面的代碼,但我的數據沒有刪除。如何通過數據ID從Arraylist中刪除數據

請檢查我的代碼,併爲其提供解決方案。

更新 當我調用此方法則編譯器讀取所有下面的代碼,但它不會從我的數組列表中刪除我的數據。我沒有收到任何錯誤。

------------------- 
    ------------------- 
    public String removebookfrmSession() 
    { 
     List<Bookdetails> books = new ArrayList<Bookdetails>(); 
     String bookid = request.getParameter("bkid");  
     Bookdetails book = dao.listBookDetailsById(Integer.parseInt(bookid)); 
     books = (ArrayList) session.get(BillTransactionBooksConstants.BOK); 
     if (books == null) books = new ArrayList<Bookdetails>(); 
     boolean already_exists = false; 
     for (Bookdetails b : books) 
     { 
      if (Integer.toString(b.getId()).equals(bookid)) 
      { 
       already_exists = true; 
       break; 
      } 
     } 
     if (book != null && already_exists ) 
     { 
      books.remove(book); 
      System.out.println("books size"+books.size()); 
      session.put(BillTransactionBooksConstants.BOK,books); 
     } 
     return SUCCESS; 
    } 

Bookdetails.java(POJO)

 package v.esoft.pojos; 

    // Generated Nov 5, 2012 9:37:14 PM by Hibernate Tools 3.4.0.CR1 

    import java.util.Date; 
    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import static javax.persistence.GenerationType.IDENTITY; 
    import javax.persistence.Id; 
    import javax.persistence.Table; 
    import javax.persistence.Temporal; 
    import javax.persistence.TemporalType; 

    /** 
    * Bookdetails generated by hbm2java 
    */ 
    @Entity 
    @Table(name = "bookdetails", catalog = "vbsoftware") 
    public class Bookdetails implements java.io.Serializable { 

     private Integer id; 
     private String isbn; 
     private String bookTitile; 
     private String authFirstname; 
     private String authLastname; 
     private String editionYear; 
     private Integer subjectId; 
     private Integer coverId; 
     private Integer languageId; 
     private String publisherName; 
     private Integer editionId; 
     private Float price; 
     private String quantity; 
     private String description; 
     private Integer locationId; 
     private String remarks; 
     private String img1; 
     private String img2; 
     private String videoUrl; 
     private Integer createrId; 
     private Date createdDate; 
     private Integer updateId; 
     private Date updatedDate; 

     public Bookdetails() { 
     } 

     public Bookdetails(String isbn, String bookTitile, String authFirstname, 
       String authLastname, String editionYear, Integer subjectId, 
       Integer coverId, Integer languageId, String publisherName, 
       Integer editionId, Float price, String quantity, 
       String description, Integer locationId, String remarks, 
       String img1, String img2, String videoUrl, Integer createrId, 
       Date createdDate, Integer updateId, Date updatedDate) { 
      this.isbn = isbn; 
      this.bookTitile = bookTitile; 
      this.authFirstname = authFirstname; 
      this.authLastname = authLastname; 
      this.editionYear = editionYear; 
      this.subjectId = subjectId; 
      this.coverId = coverId; 
      this.languageId = languageId; 
      this.publisherName = publisherName; 
      this.editionId = editionId; 
      this.price = price; 
      this.quantity = quantity; 
      this.description = description; 
      this.locationId = locationId; 
      this.remarks = remarks; 
      this.img1 = img1; 
      this.img2 = img2; 
      this.videoUrl = videoUrl; 
      this.createrId = createrId; 
      this.createdDate = createdDate; 
      this.updateId = updateId; 
      this.updatedDate = updatedDate; 
     } 

     @Id 
     @GeneratedValue(strategy = IDENTITY) 
     @Column(name = "id", unique = true, nullable = false) 
     public Integer getId() { 
      return this.id; 
     } 

     public void setId(Integer id) { 
      this.id = id; 
     } 

     @Column(name = "isbn", length = 90) 
     public String getIsbn() { 
      return this.isbn; 
     } 

     public void setIsbn(String isbn) { 
      this.isbn = isbn; 
     } 

     @Column(name = "book_titile") 
     public String getBookTitile() { 
      return this.bookTitile; 
     } 

     public void setBookTitile(String bookTitile) { 
      this.bookTitile = bookTitile; 
     } 

     @Column(name = "auth_firstname", length = 120) 
     public String getAuthFirstname() { 
      return this.authFirstname; 
     } 

     public void setAuthFirstname(String authFirstname) { 
      this.authFirstname = authFirstname; 
     } 

     @Column(name = "auth_lastname", length = 120) 
     public String getAuthLastname() { 
      return this.authLastname; 
     } 

     public void setAuthLastname(String authLastname) { 
      this.authLastname = authLastname; 
     } 

     @Column(name = "edition_year", length = 20) 
     public String getEditionYear() { 
      return this.editionYear; 
     } 

     public void setEditionYear(String editionYear) { 
      this.editionYear = editionYear; 
     } 

     @Column(name = "subject_id") 
     public Integer getSubjectId() { 
      return this.subjectId; 
     } 

     public void setSubjectId(Integer subjectId) { 
      this.subjectId = subjectId; 
     } 

     @Column(name = "cover_id") 
     public Integer getCoverId() { 
      return this.coverId; 
     } 

     public void setCoverId(Integer coverId) { 
      this.coverId = coverId; 
     } 

     @Column(name = "language_id") 
     public Integer getLanguageId() { 
      return this.languageId; 
     } 

     public void setLanguageId(Integer languageId) { 
      this.languageId = languageId; 
     } 

     @Column(name = "publisher_name", length = 70) 
     public String getPublisherName() { 
      return this.publisherName; 
     } 

     public void setPublisherName(String publisherName) { 
      this.publisherName = publisherName; 
     } 

     @Column(name = "edition_id") 
     public Integer getEditionId() { 
      return this.editionId; 
     } 

     public void setEditionId(Integer editionId) { 
      this.editionId = editionId; 
     } 

     @Column(name = "price", precision = 12, scale = 0) 
     public Float getPrice() { 
      return this.price; 
     } 

     public void setPrice(Float price) { 
      this.price = price; 
     } 

     @Column(name = "quantity", length = 40) 
     public String getQuantity() { 
      return this.quantity; 
     } 

     public void setQuantity(String quantity) { 
      this.quantity = quantity; 
     } 

     @Column(name = "description", length = 65535) 
     public String getDescription() { 
      return this.description; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

     @Column(name = "location_id") 
     public Integer getLocationId() { 
      return this.locationId; 
     } 

     public void setLocationId(Integer locationId) { 
      this.locationId = locationId; 
     } 

     @Column(name = "remarks", length = 65535) 
     public String getRemarks() { 
      return this.remarks; 
     } 

     public void setRemarks(String remarks) { 
      this.remarks = remarks; 
     } 

     @Column(name = "img1") 
     public String getImg1() { 
      return this.img1; 
     } 

     public void setImg1(String img1) { 
      this.img1 = img1; 
     } 

     @Column(name = "img2") 
     public String getImg2() { 
      return this.img2; 
     } 

     public void setImg2(String img2) { 
      this.img2 = img2; 
     } 

     @Column(name = "video_url", length = 65535) 
     public String getVideoUrl() { 
      return this.videoUrl; 
     } 

     public void setVideoUrl(String videoUrl) { 
      this.videoUrl = videoUrl; 
     } 

     @Column(name = "creater_id") 
     public Integer getCreaterId() { 
      return this.createrId; 
     } 

     public void setCreaterId(Integer createrId) { 
      this.createrId = createrId; 
     } 

     @Temporal(TemporalType.TIMESTAMP) 
     @Column(name = "created_date", length = 19) 
     public Date getCreatedDate() { 
      return this.createdDate; 
     } 

     public void setCreatedDate(Date createdDate) { 
      this.createdDate = createdDate; 
     } 

     @Column(name = "update_id") 
     public Integer getUpdateId() { 
      return this.updateId; 
     } 

     public void setUpdateId(Integer updateId) { 
      this.updateId = updateId; 
     } 

     @Temporal(TemporalType.TIMESTAMP) 
     @Column(name = "updated_date", length = 19) 
     public Date getUpdatedDate() { 
      return this.updatedDate; 
     } 

     public void setUpdatedDate(Date updatedDate) { 
      this.updatedDate = updatedDate; 
     } 

    } 
+0

你應該使用'Iterator'然後調用'remove'。或者根據'bookid'提供等號 –

+0

你面臨什麼問題?任何異常,錯誤或意外輸出?請張貼相關的東西。你爲什麼這樣說: - 'return SUCCESS'。我在那裏看到一個未定義的變量。 –

+0

@RohitJain我更新了。請檢查我的信息。 – user1726508

回答

2

Bookdetails類必須使用List#remove(Object o)方法實現public boolean equals(Object o)方法。否則,您應該像@juergend所說的那樣自己處理對象刪除。

實施equals方法是這樣的:

public class Bookdetails { 
    private int id; 
    //other attributes and methods... 

    @Override 
    public boolean equals(Object o) { 
     if (o instanceof Bookdetails) { 
      Bookdetails oBookdetails = (Bookdetails)o; 
      return (this.id == oBookdetails.id); 
     } 
     return false; 
    } 
} 

現在books.remove(book);將不需要的for (Bookdetails b : books)循環的工作。

+0

我寫了這個方法'@Override public boolean equals(Object o){'在我的Bookdetails pojo類中,但是IDE顯示錯誤錯誤:**這條線上有多個標記 \t - 此方法必須返回類型結果 \t布爾值 \t - 覆蓋java.lang.Object.equals ** – user1726508

+0

@ user1726508答案已更新,我的錯誤。 –

1

默認相等基於reference,因此您需要將book指定爲list的引用,以便remove可以使用。或者你也可以覆蓋equals但我不能肯定是否可以在bookId

for (Bookdetails b : books) 
{ 
    if (Integer.toString(b.getId()).equals(bookid)) 
    { 
     already_exists = true; 
     book= b; 
     break; 
    } 
} 

提供平等然後當你調用remove因爲引用相等它會刪除book

更新:

如果它的POJO和primary key那麼你可以很容易地在primary key上實現等於id。無需遍歷列表。