【问题标题】:java incompatible types class data typesjava不兼容的类型类数据类型
【发布时间】:2016-03-29 18:52:10
【问题描述】:

嗨,这是我在这里的第一篇文章,我遇到了麻烦。我想根据一个名为 Account 的预制数据类型类创建一个类。我的主要问题在于 public int findAccountByAcctNumber(int acctNumber) 和 公共帐户 removeAccount(int index)。难点在于如何用不同的数据类型创建这些方法?

import java.util.*;

public class Bank
{

    private ArrayList<Account> Accounts;
    private int currentSize;
    private String bankName;


    public Bank(String name)
    {
        bankName = name;
        Accounts = new ArrayList<Account>(0);

    }


    public void addAccount(Account acct){
        Accounts.add(acct);

    }
    public int findAccountByAcctNumber(int acctNumber){
        int tempIndex = -1;
        for(int i = 0; i < Accounts.size(); i++){
            if(Accounts.get(i) == acctNumber){
                tempIndex = i;
            }
        }
        return tempIndex;

    }
    public Account removeAccount(int index){

        Accounts.remove(index);
        Account 
        return index;

    }
    public String toString(){
        String output = "";

        output += bankName + "/n";
        for(int i = 0; i < arrlist.size(); i++){
            output += Accounts.get(i);
        }
        return output;


    }


}

【问题讨论】:

    标签: java class types


    【解决方案1】:

    您没有向我们展示 Account 类,但我猜它有一个字段 accountNumber

    您需要将输入的帐号与字段进行比较,而不是 Account 对象本身:

      public int findAccountByAcctNumber(int acctNumber){
            int tempIndex = -1;
            for(int i = 0; i < Accounts.size(); i++){
                //NOT  if(Accounts.get(i) == acctNumber){ ->
                if(Accounts.get(i).getAccountNumber() == acctNumber){
                    tempIndex = i;
                }
            }
            return tempIndex;
    
        }
    

    remove 很简单(ArrayList 已经实现了这个功能):

    public Account removeAccount(int index){
        return Accounts.remove(index);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多