繁体   English   中英

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

[英]Java Type mismatch: cannot convert from java.lang.Object

我正在研究蓝色鹈鹕Java教科书并使用drjava。 我目前正在研究第43课的大型雄鹿项目,基本上我必须使用此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 < aryLst.size( ); j++)  
    {
      //? Step through the remaining objects and decide which one has    
      //largest balance (compare each balance to maxBalance) 
      BankAccount na = 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 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<BankAccount> aryLst = new ArrayList<BankAccount>( );

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


另一种方法(如果您使用的是Java 5或更高版本,则不建议使用)是在访问列表时强制转换对象。

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

您正在创建原始对象的数组列表。 而是应该创建BankAccount对象的arraylist。 改变这个

List aryLst = new ArrayList( ); 

List <BankAccount>aryLst = new ArrayList<BankAccount>( ); 

这是我键入的内容,以得到项目本身所述的正确打印输出。

    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 + ".");

    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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