簡體   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