交易java.lang.ClassCastException:java.util.ArrayList不能强制转换为checkingAccount.BankEntry

交易java.lang.ClassCastException:java.util.ArrayList不能强制转换为checkingAccount.BankEntry,java,Java,正在处理ClassCastException,但尚未找到答案 我应该使用ArraList解析元素,然后将元素转换为StringsList吗 如果能帮我解决这个问题,我将不胜感激 这是导致问题的代码,但我不知道如何修复它: 我的servlet的doGet方法中有这些方法 public void init(ServletConfig config) throws ServletException { super.init(config); List<BankAcc

正在处理ClassCastException,但尚未找到答案

我应该使用ArraList解析元素,然后将元素转换为StringsList吗

如果能帮我解决这个问题,我将不胜感激

这是导致问题的代码,但我不知道如何修复它:

我的servlet的doGet方法中有这些方法

public void init(ServletConfig config) throws ServletException {

        super.init(config);


    List<BankAccountEntry> entries = new ArrayList<BankAccountEntry>();

    // Pre-populate the guestbook with a few entries
    entries.add(new BankAccountEntry("efd", "df", 100.00, 24.40));

    entries.add(new BankAccountEntry("dfd", "dfd", 45.00, 25.50));

    entries.add(new BankAccountEntry("ssd", "dfd", 50.00, 65.50));


    getServletContext().setAttribute("entries", entries);
}




List<BankAccountEntry> entries = (List<BankAccountEntry>)     context.getAttribute("entries");






 String query = request.getParameter("query"); 




    This past causes the error, but not sure why:
        if (query == null ||  ((BankAccountEntry) entries).getDescription().contains( query.trim() ) || ((BankAccountEntry) entries).getName().contains( query.trim() ) ) {
            // Send the User back to the Guest Book page
        }

        String search = request.getParameter("query");

        if (query == null ||  ((BankAccountEntry)    entries).getDescription().contains( query.trim() ) || ((BankAccountEntry)     entries).getName().contains( query.trim() ) ) {
            // Send the User back to the Guest Book page
        }   
public void init(ServletConfig配置)抛出ServletException{
super.init(config);
列表项=新的ArrayList();
//用几个条目预先填充留言簿
添加(新银行账户条目(“efd”、“df”、100.00、24.40));
添加(新银行账户条目(“dfd”、“dfd”、45.00、25.50));
添加(新的银行账户条目(“ssd”、“dfd”、50.00、65.50));
getServletContext().setAttribute(“条目”,条目);
}
列表条目=(列表)context.getAttribute(“条目”);
字符串查询=request.getParameter(“查询”);
此过去导致错误,但不确定原因:
if(query==null | | |((BankAccountEntry)条目).getDescription().contains(query.trim())| |((BankAccountEntry)条目).getName().contains(query.trim())){
//将用户发送回留言簿页面
}
字符串搜索=request.getParameter(“查询”);
if(query==null | | |((BankAccountEntry)条目).getDescription().contains(query.trim())| |((BankAccountEntry)条目).getName().contains(query.trim())){
//将用户发送回留言簿页面
}   
根据您的代码

List<BankAccountEntry> entries
您正试图将其转换为单数
BankAccountEntry

也许在这段代码中,您应该迭代列表

如果你想迭代

for (BankAccountEntry bae : entries) {

   if (bae.getDescription().contains(query.trim()) {
      // whatever
   }
}

我理解这里各种解决方案的这一部分,但我不确定如何实施它。你能帮我实现吗?Eclipse建议使用该模式。如果它是Eclipse推荐的,为什么会错呢?在您的代码中,您向列表中添加了3个条目,那么当您执行
getDescription
时,您希望将它与哪个条目进行比较?都是吗?我编辑了我的答案来展示如何迭代mbat,是的,我想在使用servlet和搜索表单执行搜索功能时与所有答案进行比较!我没有意识到我的错误。谢谢。一般来说,当Eclipse建议进行强制转换时,不要相信它,因为Eclipse不理解您在做什么。仅当您无论如何都要手动执行时才执行此操作。
for (BankAccountEntry bae : entries) {

   if (bae.getDescription().contains(query.trim()) {
      // whatever
   }
}