如何从数组列表中添加和检索?

时间:2016-04-24 04:51:59

标签: java

我正在创建一个模拟银行业务程序,并希望使用arraylists来跟踪诸如姓氏,账户余额等内容,然后我想用print语句显示这些值。我该怎么做?

1 个答案:

答案 0 :(得分:1)

    ArrayList<Account> accounts = new ArrayList<>();

    Account exampleAccount = new Account();
    exampleAccount.setFirstName("John");
    exampleAccount.setLastName("Doe");
    exampleAccount.setBalance(101.13d);

    accounts.add(exampleAccount);

    for(Account account : accounts) {
        System.out.println("Name: " + account.getFirstName() + " " + account.getLastName() + 
                ".  Balance: " + account.getBalance());
    // Or override Account's toString() method and do System.out.println(account.toString()) 
    }