Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为对象实现arrayList_Java_Oop_Arraylist - Fatal编程技术网

Java 如何为对象实现arrayList

Java 如何为对象实现arrayList,java,oop,arraylist,Java,Oop,Arraylist,我试图将值传递给名为“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 in

我试图将值传递给名为“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

  }

}

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

我的主代码

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.
          }
}
公共类分配{
公共静态void main(字符串[]args){
字符串类型;
扫描仪输入=新扫描仪(System.in);
System.out.println(“您想创建什么类型的帐户?:”;
type=input.nextLine();
说明某人;
sb=新的sbaccount();
//sb.setData(“约斯佩”,“0563994”,50000);
sb.setData(“Joohn”,“009734”,50000);
sb.setData(“希望”,“05634”,50000);
ArrayList usera=新的ArrayList();
用户a.添加((sbaccount)sb);
用户a.添加((sbaccount)sb);
对于(int i=0;i
还有我的子类“sbaccount”的代码

最终类sbaccount扩展帐户{
int-total=0;
公共帐户(){
超级();
}
公共void setData(字符串名称、字符串编号、整数金额){
this.name=名称;
这个。数字=数字;
这个。金额=金额;
}
公共void showData(){
System.out.println(“账户所有者:+名称”);
System.out.println(“账号:“+编号”);
System.out.println(“您的新金额为”+newbalance);
System.out.println(“您的帐户余额为:$”+总计);
返回;
}
整数存款(整数货币){
新余额=货币+金额;
System.out.println(“Hi”+名称
+“\n计数编号:”+编号
+“\n您已存入:$”+货币);
System.out.println(“\n您的帐户余额现在为:“+newbalance”);
返回新平衡;
}
无效撤回(int撤回){
int-total=0;
总计=余额-提取;

如果(total首先,我建议您按照

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

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

i、 用电子邮件代替写作

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);

我想你忘了说你的问题是什么。嗨@Amongalen!我的问题是在编译之后,当我试图从数组列表中检索数据时,我无法检索已经存储的数据。1)我的对象似乎没有正确地将其存储在数组中?它的值不断被覆盖。2)如何从数组列表中检索数据?谢谢@Abhinaba!我的问题是在编译后,当我尝试从数组列表中检索数据时,我无法检索已存储的数据。1)我的对象似乎没有正确地将其存储在数组中数组?值不断被覆盖。2)如何从数组列表中检索数据?@olawalegorge我已经更新了我对这个问题的答案。您正在以正确的方式从数组列表中检索数据,尽管还有许多其他方法可以这样做。
         Account sb1 = new Sbaccount();
         sb1.setData("Joohn", "009734",50000);
         Account sb2 = new Sbaccount();
         sb2.setData("hope", "05634",50000);

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