0

I have a program which requires me to calculate a total price for a flight booking and I need to multiply that total by the number of passengers in an array list..

How do I do this if the array list that is being created is empty?

Or do I need to set a number on the number of elements in the array list?

Hope someone can help thanks.

1 Answers1

2

ArrayList is a collection in Java that will automatically expand as you add elements to it.

Here's a simple example of how to compute the total revenue from a set ticket price:

BigDecimal ticketPrice = new BigDecimal("500.00");
List<Passenger> list = new ArrayList<>();
list.add(new Passenger(...);
list.add(new Passenger(...);
...
BigDecimal totalPrice = ticketPrice.multiply(new BigDecimal(list.size()));
Robert Dean
  • 685
  • 5
  • 16