127k views
5 votes
Suppose a supermarket needs to store the number of units sold of a particular product in their 20 locations. Which of the following would be the best data structure to use, and why?

a) An ArrayList of Double because the number of products is known.
b) An ArrayList of Integer because the number of locations is known.
c) An ArrayList of Integer because the number of locations is unknown.
d) An array of int because the number of locations is known.

User Gingo
by
7.7k points

1 Answer

7 votes

Final answer:

The best data structure for storing the number of units sold across 20 locations is an array of int, as it provides a fixed-size, memory-efficient way to store whole number counts.

Step-by-step explanation:

If a supermarket needs to store the number of units sold of a particular product across their 20 locations, the best data structure to use would be d) an array of int because the number of locations is known. This data structure is static in size, which makes it efficient for storing a fixed number of integer values, such as the count of products sold at each location.

Unlike an ArrayList, which is dynamically resizable and typically used when the number of elements can change, an array is a simpler and more memory-efficient choice when the size is fixed. Additionally, we choose an array of int rather than Double because the number of units sold would be a whole number.

User Amir Jalali
by
8.6k points