Java类型不匹配:无法从java.lang.Object转换

我正在使用蓝鹈鹕java教科书并使用drjava。 我目前正在研究第43课Big Bucks项目,基本上我必须使用这个BankAccount类:

public class BankAccount { public BankAccount(String nm, double amt) { name = nm; balance = amt; } public void deposit(double dp) { balance = balance + dp; } public void withdraw(double wd) { balance = balance - wd; } public String name; public double balance; } 

并创建另一个类,允许我输入多个帐户,将它们存储在一个数组列表中,然后确定哪个帐户具有最大的余额。 这是我到目前为止的代码:

 import java.io.*; import java.util.*; //includes ArrayList import java.text.*; //for NumberFormat public class BigBucks { public static void main(String args[]) { NumberFormat formatter = NumberFormat.getNumberInstance( ); formatter.setMinimumFractionDigits(2); formatter.setMaximumFractionDigits(2); String name; List aryLst = new ArrayList( ); do { Scanner kbReader = new Scanner(System.in); System.out.print("Please enter the name to whom the account belongs. (\"Exit\" to abort)"); name = kbReader.nextLine( ); if( !name.equalsIgnoreCase("EXIT") ) { System.out.print("Please enter the amount of the deposit. "); double amount = kbReader.nextDouble(); System.out.println(" "); //gives an eye-pleasing blank line BankAccount myAccount = new BankAccount(name,amount); aryLst.add(myAccount); //add account to array list } }while(!name.equalsIgnoreCase("EXIT")); //Search aryList and print out the name and amount of the largest bank account BankAccount ba = aryLst.get(0);//get first account in the list double maxBalance = ba.balance; String maxName = ba.name; for(int j = 1; j  maxBalance) { maxBalance = nBalance; maxName = nName; } //? Step through the remaining objects and decide which one has largest balance (compare each balance to maxBalance) //? } System.out.println("The accouint with the largest balance belongs to "+ maxName + "."); System.out.println("The amount is $" + maxBalance + "."); } } 

但是每次我编译它都会收到此错误

 Type mismatch: cannot convert from java.lang.Object to BankAccount 

在第28和35行。为什么我会收到此错误,我该如何解决? 任何帮助表示赞赏。

您将BankAccount对象存储在普通列表中。

 List aryLst = new ArrayList( ); 

更改它以指定BankAccount对象的列表。

 List aryLst = new ArrayList( ); 

以这种方式使用generics时 ,尝试将除BankAccount之外的任何内容添加到列表中将是编译器错误,但是在访问列表元素时不必转换对象。


替代方案(如果您使用的是Java 5或更高版本,则不推荐)将在您访问列表时强制转换对象。

 BankAccount ba = (BankAccount)aryLst.get(0); 

您正在创建原始对象的arraylist。 相反,您应该创建BankAccount对象的arraylist。 改变这个

 List aryLst = new ArrayList( ); 

 List aryLst = new ArrayList( ); 

下面是我输入的内容,以获得项目本身所述的正确打印输出。

  public static void main (String[] args) { NumberFormat formatter = NumberFormat.getNumberInstance(); formatter.setMinimumFractionDigits(2); formatter.setMaximumFractionDigits(2); String name; ArrayList aryLst = new ArrayList(); do { Scanner in = new Scanner(System.in); out.print("Please enter the name to whom the account belongs. (\"Exit\" to abort)"); name = in.nextLine(); if(!name.equalsIgnoreCase("EXIT")) { out.print("Please enter the amount of the deposit. " ); double amount = in.nextDouble(); out.println(" "); // gives an eye-pleasing bank line BankAccount acct = new BankAccount(name, amount); aryLst.add(acct); } }while(!name.equalsIgnoreCase("EXIT")); //Search aryList and print out the name and amount of the largest bank account BankAccount ba = (BankAccount) aryLst.get(0); double maxBalance = ba.balance; String maxName = ba.name; for(int j = 1; j < aryLst.size( ); j++) { //? Step through the remaining objects and decide which one has //largest balance (compare each balance to maxBalance) BankAccount na = (BankAccount) aryLst.get(j); double nBalance = na.balance; String nName = na.name; if(nBalance > maxBalance) { maxBalance = nBalance; maxName = nName; } //? Step through the remaining objects and decide which one has largest balance (compare each balance to maxBalance) //? } System.out.println("The account with the largest balance belongs to "+ maxName + "."); System.out.println("The amount is $" + maxBalance + "."); } 

我希望这会有所帮助,我在同一个项目中遇到了麻烦,我能做的最少就是帮助你。