15.7k views
1 vote
import java.util.ArrayList; // Needed for ArrayList class /** This program demonstrates how to store BankAccount objects in an ArrayList. */ public class ArrayListDemo6 { public static void main(String[] args) { // Create an ArrayList to hold BankAccount objects. ArrayList list = new ArrayList(); // Add three BankAccount objects to the ArrayList. list.add(new bankAccount(100.0)); list.add(new bankAccount(500.0)); list.add(new bankAccount(1500.0)); // Display each item. for (int index = 0; index < array.size(); index++) { BankAccount account = list.get(index); System.out.println("Account at index " + index + "\\Balance: " + account.getBalance()); } } }

User Grimthorr
by
6.1k points

1 Answer

4 votes

Answer:

import java.util.ArrayList; // Needed for ArrayList class

/** This program demonstrates how to store BankAccount objects in an ArrayList. */

// Class ArrayListDemo6 is defined

public class ArrayListDemo6 {

// main method that begin program execution

public static void main(String[] args) {

// Create an ArrayList to hold BankAccount objects.

// The arraylist is called list

ArrayList list = new ArrayList();

// Add three BankAccount objects to the ArrayList.

list.add(new bankAccount(100.0)); list.add(new bankAccount(500.0)); list.add(new bankAccount(1500.0)); // Display each item using a for loop

for (int index = 0; index < array.size(); index++)

{ BankAccount account = list.get(index);

System.out.println("Account at index " + index + "\\Balance: " + account.getBalance());

}

}

}

Step-by-step explanation:

The code is written in Java and more comments has been added to make it more explanatory.

The code snippet is a demonstration on Arraylist.

First Arraylist is imported. Then, ArrayListDemo6 class is defined. Then the main method which begin program execution.

Inside the main method, a new Arraylist is created called list.

Then we add three BankAccount object to the list.

Lastly, we use a for loop to display each added BankAccount object.

User Mickzer
by
4.9k points