0
0
Javaprogramming~15 mins

Common array operations in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeCommon array operations
📖 Scenario: You are working on a simple inventory system for a small store. The store keeps track of the quantities of three products in an array.
🎯 Goal: You will create an array to hold product quantities, set a threshold for low stock, find which products are low in stock, and print those products.
📋 What You'll Learn
Create an integer array called quantities with values 10, 5, and 2
Create an integer variable called lowStockThreshold and set it to 5
Use a for loop with variable i to find indices of products with quantity less than lowStockThreshold and store these indices in an integer list called lowStockProducts
Print the lowStockProducts list
💡 Why This Matters
🌍 Real World
Stores and warehouses often track product quantities to know when to reorder items.
💼 Career
Understanding arrays and loops is essential for managing collections of data in software development.
Progress0 / 4 steps
1
Create the product quantities array
Create an integer array called quantities with these exact values: 10, 5, and 2.
Java
💡 Need a hint?

Use curly braces to list the values inside the array.

2
Set the low stock threshold
Create an integer variable called lowStockThreshold and set it to 5.
Java
💡 Need a hint?

Use int to declare the variable and assign the value 5.

3
Find products with low stock
Create an ArrayList<Integer> called lowStockProducts. Then use a for loop with variable i from 0 to quantities.length - 1. Inside the loop, if quantities[i] is less than lowStockThreshold, add i to lowStockProducts.
Java
💡 Need a hint?

Remember to import java.util.ArrayList at the top.

4
Print the low stock product indices
Write a System.out.println statement to print the lowStockProducts list.
Java
💡 Need a hint?

The output should show the list of indices where the quantity is less than 5.