【问题标题】:how to implement arrayList for objects如何为对象实现arrayList
【发布时间】:2020-07-03 13:50:26
【问题描述】:

我正在尝试将值传递给名为“Account”的超级类,然后将每个 obj 存储到一个数组列表中。

我的班级账户代码

abstract class account{
  String number;
  String  name;
  int amount;
  int newbalance;
  static final int balance = 1000;
  int bal;

  public  account(){
    // used to store the parameter passed into constructor

  }



  int deposit(int i) {
    return i;

  }

  void withdrawal(int i){

  }

  public void setData(String string, String string2, int i) {
    // TODO Auto-generated method stub

  }

  public void showData() {
    // TODO Auto-generated method stub

  }

}

在我的主要方法中,我想创建一个对象,然后传入要存储在数组列表中的 name 、 number 和 amount 的值,因为我想要为它们存储多个具有不同名称的对象 number 和 amount .然后我想访问子类“Sbaccount”并更新“newbalance”变量

我的 main 代码

public class oopassignment {

    public static void main(String[] args) {
        String type;
        Scanner input = new Scanner (System.in);
        System.out.println("What type of account do you want to create? :");
        type=input.nextLine();
        
         Account sb;
          sb = new sbaccount();
          
          //sb.setData("Jospeh", "0563994",50000);
          sb.setData("Joohn", "009734",50000);
          sb.setData("hope", "05634",50000);
          ArrayList <sbaccount> usera = new ArrayList<sbaccount>();
          usera.add((sbaccount) sb);
         usera.add((sbaccount) sb);
          for (int i = 0; i < usera.size(); i++){
        usera.get(i).showData();
           //userb.
          }
}

还有我的子类“sbaccount”的代码

final class sbaccount extends Account {
  int total = 0;

  public  sbaccount(){
    super ();

  }
  public void setData(String  name, String  number, int  amount){

    this.name = name;
    this.number= number;
    this.amount= amount;
  }
  public void showData(){
    System.out.println("Account Owner: "+name);
    System.out.println("Account number: "+ number);
    System.out.println("Your New Amount is "+ newbalance);

    System.out.println("Your Account Balance is: $"+ total);
    return;
  }
  int deposit ( int money){
    newbalance = money + amount;

    System.out.println("Hi "+ name
        +" \nAccountnumber: "+ number
        +" \nYou have deposited :$"+money);
    System.out.println("\nYour Account balance is now :"+ newbalance);
    return newbalance;

  }
  void withdrawal(int withdraw){
    int total = 0;
    total = bal - withdraw;
    if (total <= balance){
      System.out.println("Your balance is too low for withdrawal!");

    }
    else{
      System.out.println("\nHi "+ name
          +" \nAccountnumber: "+ number
          +" \nYou have Withdrawn :$"+withdraw);
      System.out.println("\nYour Account balance is now :"+ total);
    }

  }

}

【问题讨论】:

  • 我想你忘了说你的问题是什么。
  • 嗨@Amongalen!我的问题是在我编译后,当我尝试从数组列表中检索数据时,我无法检索已经存储的数据。 1)我的对象似乎没有正确地存储在数组中?值它不断被覆盖。 2)如何从数组列表中检索数据?

标签: java oop arraylist


【解决方案1】:

首先,我建议您按照official naming conventions 将每个类名的首字母大写

在那之后,我在您的代码中看不到任何编译错误。就您的问题而言,您已经成功实现了对象的arraylist。

还有一件事,我想说的是,您不需要在添加到列表之前强制转换对象,因为它是父类型的子类。

即代替写作

usera.add((sbaccount) sb);

简单地使用

usera.add( sb);

编辑:

所以你的问题是为什么你的对象中的值不断被覆盖?答案很简单。那是因为你写了

         sb.setData("Joohn", "009734",50000);
          sb.setData("hope", "05634",50000);

这意味着您首先在“sb”对象中设置数据,然后在同一个“sb”对象中更新数据。

要将两个数据都存储在数组列表中,您只需要两个对象,比如 sb1 和 sb2..

         Account sb1 = new Sbaccount();
         sb1.setData("Joohn", "009734",50000);
         Account sb2 = new Sbaccount();
         sb2.setData("hope", "05634",50000);

 ....
        usera.add(sb1);
        usera.add(sb2);

【讨论】:

  • 感谢@Abhinaba!我的问题是在我编译后,当我尝试从数组列表中检索数据时,我无法检索已经存储的数据。 1)我的对象似乎没有正确地存储在数组中?值它不断被覆盖。 2)如何从数组列表中检索数据?
  • @OlawaleGeorge 我已经更新了我对这个问题的回答。您正在以正确的方式从数组列表中检索数据,尽管还有许多其他方法可以做到这一点。
猜你喜欢
  • 1970-01-01
  • 2021-12-02
  • 2020-11-12
  • 2020-03-27
  • 2017-05-24
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 2021-04-16
相关资源
最近更新 更多