Arrays & Collections
ArrayList
Dynamic Lists
Your shopping cart can't be a fixed-size array - users add and remove items constantly. ArrayList grows and shrinks automatically, handling the resizing so you can focus on your logic.
Add items to a cart
Create an ArrayList and add elements dynamically.
import java.util.ArrayList;
public class ShoppingCart {
public static void main(String[] args) {
// Create an empty shopping cart
ArrayList<String> cart = new ArrayList<>();
System.out.println("=== Shopping Session ===");
System.out.println("Cart starts empty: " + cart);
System.out.println("Items in cart: " + cart.size());
// Add items to cart
cart.add("Milk");
System.out.println("Added Milk: " + cart);
cart.add("Bread");
System.out.println("Added Bread: " + cart);
cart.add("Eggs");
System.out.println("Added Eggs: " + cart);
// Add more items
cart.add("Butter");
cart.add("Cheese");
cart.add("Yogurt");
System.out.println("Added more: " + cart);
// Insert at specific position (beginning)
cart.add(0, "Coffee");
System.out.println("Inserted Coffee at start: " + cart);
System.out.println("\n=== Final Cart ===");
System.out.println("Items: " + cart);
System.out.println("Total items: " + cart.size());
}
}
cart ← []
4public class ShoppingCart {5 public static void main(String[] args) {6 // Create an empty shopping cart //#?create7 ArrayList<String> cart→ [] = new ArrayList<>();8 9 System.out.println("=== Shopping Session ===");10 System.out.println("Cart starts empty: " + cart[]);11 System.out.println("Items in cart: " + cart.size());12 13 // Add items to cart14 cart.add("Milk"); //?add15 System.out.println("Added Milk: " + cart[Milk]);16 17 cart.add("Bread");18 System.out.println("Added Bread: " + cart[Milk, Bread]);19 20 cart.add("Eggs");21 System.out.println("Added Eggs: " + cart[Milk, Bread, Eggs]);22 23 // Add more items24 cart.add("Butter"); //@var=_,!25 cart.add("Cheese"); //@var=_,!26 cart.add("Yogurt"); //@var=_,!27 System.out.println("Added more: " + cart[Milk, Bread, Eggs, Butter, Cheese, Yogurt]); //@var=_,!28 29 // Insert at specific position (beginning)30 cart.add(0, "Coffee"); //?insert31 System.out.println("Inserted Coffee at start: " + cart[Coffee, Milk, Bread, Eggs, Butter, Cheese, Yogurt]);32 33 System.out.println("\n=== Final Cart ===");34 System.out.println("Items: " + cart[Coffee, Milk, Bread, Eggs, Butter, Cheese, Yogurt]);35 System.out.println("Total items: " + cart.size());36 }output=== Shopping Session === Cart starts empty: [] Items in cart: 0 Added Milk: [Milk] Added Bread: [Milk, Bread] Added Eggs: [Milk, Bread, Eggs] Added more: [Milk, Bread, Eggs, Butter, Cheese, Yogurt] Inserted Coffee at start: [Coffee, Milk, Bread, Eggs, Butter, Cheese, Yogurt] === Final Cart === Items: [Coffee, Milk, Bread, Eggs, Butter, Cheese, Yogurt] Total items: 7
ArrayList<String> holds strings. Use add() to append items.
Access items by position
Get an item at a specific index.
import java.util.ArrayList;
import java.util.Arrays;
public class GetItem {
public static void main(String[] args) {
// Initialize cart with items
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")
);
System.out.println("=== Shopping Cart ===");
System.out.println("Cart: " + cart);
System.out.println("Total items: " + cart.size());
// Access items by index
int index = 2;
System.out.println("\n=== Access by Index ===");
System.out.println("First item (index 0): " + cart.get(0));
System.out.println("Last item (index " + (cart.size()-1) + "): " +
cart.get(cart.size() - 1));
// Safe access with bounds check
if (index >= 0 && index < cart.size()) {
System.out.println("Item at index " + index + ": " + cart.get(index));
} else {
System.out.println("Index " + index + " is out of bounds!");
System.out.println("Valid indices: 0 to " + (cart.size() - 1));
}
// Display all with indices
System.out.println("\n=== Cart with Indices ===");
for (int i = 0; i < cart.size(); i++) {
System.out.println("[" + i + "] " + cart.get(i));
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class GetItem {
public static void main(String[] args) {
// Initialize cart with items
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")
);
System.out.println("=== Shopping Cart ===");
System.out.println("Cart: " + cart);
System.out.println("Total items: " + cart.size());
// Access items by index
int index = 10;
System.out.println("\n=== Access by Index ===");
System.out.println("First item (index 0): " + cart.get(0));
System.out.println("Last item (index " + (cart.size()-1) + "): " +
cart.get(cart.size() - 1));
// Safe access with bounds check
if (index >= 0 && index < cart.size()) {
System.out.println("Item at index " + index + ": " + cart.get(index));
} else {
System.out.println("Index " + index + " is out of bounds!");
System.out.println("Valid indices: 0 to " + (cart.size() - 1));
}
// Display all with indices
System.out.println("\n=== Cart with Indices ===");
for (int i = 0; i < cart.size(); i++) {
System.out.println("[" + i + "] " + cart.get(i));
}
}
}
cart ← [Coffee, Milk, Bread, Eggs, Butter], index ← 2
5public class GetItem {6 public static void main(String[] args) {7 // Initialize cart with items8 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Butter] = new ArrayList<>(9 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")10 );11 12 System.out.println("=== Shopping Cart ===");13 System.out.println("Cart: " + cart[Coffee, Milk, Bread, Eggs, Butter]);14 System.out.println("Total items: " + cart.size());15 16 // Access items by index //#?get17 int index→ 2 = 2; //@var=_,1018 19 System.out.println("\n=== Access by Index ===");20 System.out.println("First item (index 0): " + cart.get(0));21 System.out.println("Last item (index " + (cart.size()-1) + "): " + 22 cart.get(cart.size() - 1));output=== Shopping Cart === Cart: [Coffee, Milk, Bread, Eggs, Butter] Total items: 5 === Access by Index === First item (index 0): Coffee Last item (index 4): Butterif (index >= 0 && index < cart.size())
24// Safe access with bounds check25if (index2 >= 0 && index < cart.size()) { //?bounds26 System.out.println("Item at index " + index2 + ": " + cart.get(index));27} else {outputItem at index 2: BreadSystem.out.println(" === Cart with Indices ===");
32// Display all with indices33System.out.println("\n=== Cart with Indices ===");34for (int i = 0; i < cart.size(); i++) {output === Cart with Indices ===for (int i = 0; i < cart.size(); i++)
pass 1 of 533System.out.println("\n=== Cart with Indices ===");34for (int i0 = 0; i < cart.size(); i++) {35 System.out.println("[" + i0 + "] " + cart.get(i));36}output[0] CoffeeAll 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4
cart ← [Coffee, Milk, Bread, Eggs, Butter], index ← 10
4public class GetItem {5 public static void main(String[] args) {6 // Initialize cart with items7 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Butter] = new ArrayList<>(8 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")9 );10 11 System.out.println("=== Shopping Cart ===");12 System.out.println("Cart: " + cart[Coffee, Milk, Bread, Eggs, Butter]);13 System.out.println("Total items: " + cart.size());14 15 // Access items by index16 int index→ 10 = 10;17 18 System.out.println("\n=== Access by Index ===");19 System.out.println("First item (index 0): " + cart.get(0));20 System.out.println("Last item (index " + (cart.size()-1) + "): " + 21 cart.get(cart.size() - 1));output=== Shopping Cart === Cart: [Coffee, Milk, Bread, Eggs, Butter] Total items: 5 === Access by Index === First item (index 0): Coffee Last item (index 4): Butterelse
25 System.out.println("Item at index " + index + ": " + cart.get(index));26} else {27 System.out.println("Index " + index10 + " is out of bounds!");28 System.out.println("Valid indices: 0 to " + (cart.size() - 1));29}outputIndex 10 is out of bounds! Valid indices: 0 to 4System.out.println(" === Cart with Indices ===");
31// Display all with indices32System.out.println("\n=== Cart with Indices ===");33for (int i = 0; i < cart.size(); i++) {output === Cart with Indices ===for (int i = 0; i < cart.size(); i++)
pass 1 of 532System.out.println("\n=== Cart with Indices ===");33for (int i0 = 0; i < cart.size(); i++) {34 System.out.println("[" + i0 + "] " + cart.get(i));35}output[0] CoffeeAll 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4
Use get(index) to retrieve items. Index starts at 0.
Remove items from cart
Remove items by value or by index.
import java.util.ArrayList;
import java.util.Arrays;
public class RemoveItem {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Milk", "Butter")
);
System.out.println("=== Initial Cart ===");
System.out.println(cart);
// Remove by value (removes first occurrence)
String toRemove = "Milk";
boolean removed = cart.remove(toRemove);
System.out.println("\nRemove \"" + toRemove + "\": " + removed);
System.out.println("Cart: " + cart);
System.out.println("Note: Second 'Milk' still there!");
// Remove by index
int removeIndex = 1;
String removedItem = cart.remove(removeIndex);
System.out.println("\nRemoved at index " + removeIndex + ": " + removedItem);
System.out.println("Cart: " + cart);
// Try to remove non-existent item
String notInCart = "Pizza";
boolean result = cart.remove(notInCart);
System.out.println("\nRemove \"" + notInCart + "\": " + result);
// Remove all remaining items
System.out.println("\n=== Clearing Cart ===");
System.out.println("Before clear: " + cart.size() + " items");
cart.clear();
System.out.println("After clear: " + cart.size() + " items");
System.out.println("Cart is empty: " + cart.isEmpty());
}
}
cart ← [Coffee, Milk, Bread, Eggs, Milk, Butter], toRemove ← Milk
5public class RemoveItem {6 public static void main(String[] args) {7 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Milk, Butter] = new ArrayList<>(8 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Milk", "Butter")9 );10 11 System.out.println("=== Initial Cart ===");12 System.out.println(cart[Coffee, Milk, Bread, Eggs, Milk, Butter]);13 14 // Remove by value (removes first occurrence) //#?removeVal15 String toRemove→ Milk = "Milk"; //@var=_,!16 boolean removed→ true = cart.remove(toRemoveMilk);17 System.out.println("\nRemove \"" + toRemoveMilk + "\": " + removedtrue);18 System.out.println("Cart: " + cart[Coffee, Bread, Eggs, Milk, Butter]);19 System.out.println("Note: Second 'Milk' still there!");20 21 // Remove by index //#?removeIdx22 int removeIndex→ 1 = 1; //@var=!,_23 String removedItem→ Bread = cart.remove(removeIndex1); //@var=!,_24 System.out.println("\nRemoved at index " + removeIndex1 + ": " + removedItemBread); //@var=!,_25 System.out.println("Cart: " + cart[Coffee, Eggs, Milk, Butter]); //@var=!,_26 27 // Try to remove non-existent item28 String notInCart→ Pizza = "Pizza";29 boolean result→ false = cart.remove(notInCartPizza);30 System.out.println("\nRemove \"" + notInCartPizza + "\": " + resultfalse);31 32 // Remove all remaining items33 System.out.println("\n=== Clearing Cart ===");34 System.out.println("Before clear: " + cart.size() + " items");35 cart.clear(); //?clear36 System.out.println("After clear: " + cart.size() + " items");37 System.out.println("Cart is empty: " + cart.isEmpty());38 }output=== Initial Cart === [Coffee, Milk, Bread, Eggs, Milk, Butter] Remove "Milk": true Cart: [Coffee, Bread, Eggs, Milk, Butter] Note: Second 'Milk' still there! Removed at index 1: Bread Cart: [Coffee, Eggs, Milk, Butter] Remove "Pizza": false === Clearing Cart === Before clear: 4 items After clear: 0 items Cart is empty: true
remove("item") by value, remove(0) by index. Returns the removed element.
Check if item exists
Test whether an item is in the list.
import java.util.ArrayList;
import java.util.Arrays;
public class ContainsCheck {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")
);
System.out.println("=== Shopping Cart ===");
System.out.println(cart);
// Check if item exists
String searchItem = "Milk";
System.out.println("\n=== Membership Check ===");
if (cart.contains(searchItem)) {
System.out.println("✓ \"" + searchItem + "\" is in the cart");
// Find its position
int position = cart.indexOf(searchItem);
System.out.println(" Position: index " + position);
} else {
System.out.println("✗ \"" + searchItem + "\" is NOT in the cart");
System.out.println(" Would you like to add it?");
}
// Check multiple items
System.out.println("\n=== Shopping List Check ===");
String[] shoppingList = {"Milk", "Eggs", "Cheese", "Bread", "Yogurt"};
ArrayList<String> needToBuy = new ArrayList<>();
ArrayList<String> alreadyHave = new ArrayList<>();
for (String item : shoppingList) {
if (cart.contains(item)) {
alreadyHave.add(item);
} else {
needToBuy.add(item);
}
}
System.out.println("Already in cart: " + alreadyHave);
System.out.println("Still need to buy: " + needToBuy);
}
}
import java.util.ArrayList;
import java.util.Arrays;
public class ContainsCheck {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")
);
System.out.println("=== Shopping Cart ===");
System.out.println(cart);
// Check if item exists
String searchItem = "Pizza";
System.out.println("\n=== Membership Check ===");
if (cart.contains(searchItem)) {
System.out.println("✓ \"" + searchItem + "\" is in the cart");
// Find its position
int position = cart.indexOf(searchItem);
System.out.println(" Position: index " + position);
} else {
System.out.println("✗ \"" + searchItem + "\" is NOT in the cart");
System.out.println(" Would you like to add it?");
}
// Check multiple items
System.out.println("\n=== Shopping List Check ===");
String[] shoppingList = {"Milk", "Eggs", "Cheese", "Bread", "Yogurt"};
ArrayList<String> needToBuy = new ArrayList<>();
ArrayList<String> alreadyHave = new ArrayList<>();
for (String item : shoppingList) {
if (cart.contains(item)) {
alreadyHave.add(item);
} else {
needToBuy.add(item);
}
}
System.out.println("Already in cart: " + alreadyHave);
System.out.println("Still need to buy: " + needToBuy);
}
}
cart ← [Coffee, Milk, Bread, Eggs, Butter], searchItem ← Milk
5public class ContainsCheck {6 public static void main(String[] args) {7 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Butter] = new ArrayList<>(8 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")9 );10 11 System.out.println("=== Shopping Cart ===");12 System.out.println(cart[Coffee, Milk, Bread, Eggs, Butter]);13 14 // Check if item exists //#?contains15 String searchItem→ Milk = "Milk"; //@var=_,Pizza16 17 System.out.println("\n=== Membership Check ===");18 if (cart.contains(searchItem)) {output=== Shopping Cart === [Coffee, Milk, Bread, Eggs, Butter] === Membership Check ===position ← 1
17System.out.println("\n=== Membership Check ===");18if (cart.contains(searchItemMilk)) {19 System.out.println("✓ \"" + searchItemMilk + "\" is in the cart");20 21 // Find its position22 int position→ 1 = cart.indexOf(searchItemMilk); //?index23 System.out.println(" Position: index " + position1);24} else {output✓ "Milk" is in the cart Position: index 1needToBuy ← [], alreadyHave ← []
29// Check multiple items30System.out.println("\n=== Shopping List Check ===");31String[] shoppingList = {"Milk", "Eggs", "Cheese", "Bread", "Yogurt"};3233ArrayList<String> needToBuy→ [] = new ArrayList<>();34ArrayList<String> alreadyHave→ [] = new ArrayList<>();output === Shopping List Check ===for (String item : shoppingList)
pass 1 of 536for (String itemMilk : shoppingList) {37 if (cart.contains(item)) {All 5 passes — pass 1 is the card above pass item1 Milk 2 Eggs 3 Cheese 4 Bread 5 Yogurt if (cart.contains(item))
pass 1 of 336for (String item : shoppingList) {37 if (cart.contains(itemMilk)) {38 alreadyHave.add(itemMilk);39 } else {All 3 passes — pass 1 is the card above pass item1 Milk 2 Eggs 3 Bread else
pass 1 of 238 alreadyHave.add(item);39} else {40 needToBuy.add(itemCheese);41}else
pass 2 of 238 alreadyHave.add(item);39} else {40 needToBuy.add(itemYogurt);41}System.out.println("Already in cart: " + alreadyHave);
44 System.out.println("Already in cart: " + alreadyHave[Milk, Eggs, Bread]);45 System.out.println("Still need to buy: " + needToBuy[Cheese, Yogurt]);46}outputAlready in cart: [Milk, Eggs, Bread] Still need to buy: [Cheese, Yogurt]
cart ← [Coffee, Milk, Bread, Eggs, Butter], searchItem ← Pizza
4public class ContainsCheck {5 public static void main(String[] args) {6 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Butter] = new ArrayList<>(7 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")8 );9 10 System.out.println("=== Shopping Cart ===");11 System.out.println(cart[Coffee, Milk, Bread, Eggs, Butter]);12 13 // Check if item exists14 String searchItem→ Pizza = "Pizza";15 16 System.out.println("\n=== Membership Check ===");17 if (cart.contains(searchItem)) {output=== Shopping Cart === [Coffee, Milk, Bread, Eggs, Butter] === Membership Check ===else
22 System.out.println(" Position: index " + position);23} else {24 System.out.println("✗ \"" + searchItemPizza + "\" is NOT in the cart");25 System.out.println(" Would you like to add it?");26}output✗ "Pizza" is NOT in the cart Would you like to add it?needToBuy ← [], alreadyHave ← []
28// Check multiple items29System.out.println("\n=== Shopping List Check ===");30String[] shoppingList = {"Milk", "Eggs", "Cheese", "Bread", "Yogurt"};3132ArrayList<String> needToBuy→ [] = new ArrayList<>();33ArrayList<String> alreadyHave→ [] = new ArrayList<>();output === Shopping List Check ===for (String item : shoppingList)
pass 1 of 535for (String itemMilk : shoppingList) {36 if (cart.contains(item)) {All 5 passes — pass 1 is the card above pass item1 Milk 2 Eggs 3 Cheese 4 Bread 5 Yogurt if (cart.contains(item))
pass 1 of 335for (String item : shoppingList) {36 if (cart.contains(itemMilk)) {37 alreadyHave.add(itemMilk);38 } else {All 3 passes — pass 1 is the card above pass item1 Milk 2 Eggs 3 Bread else
pass 1 of 237 alreadyHave.add(item);38} else {39 needToBuy.add(itemCheese);40}else
pass 2 of 237 alreadyHave.add(item);38} else {39 needToBuy.add(itemYogurt);40}System.out.println("Already in cart: " + alreadyHave);
43 System.out.println("Already in cart: " + alreadyHave[Milk, Eggs, Bread]);44 System.out.println("Still need to buy: " + needToBuy[Cheese, Yogurt]);45}outputAlready in cart: [Milk, Eggs, Bread] Still need to buy: [Cheese, Yogurt]
contains() returns boolean. Useful before processing.
Loop through all items
Iterate over all elements in the ArrayList.
import java.util.ArrayList;
import java.util.Arrays;
public class IterateAll {
public static void main(String[] args) {
ArrayList<String> cart = new ArrayList<>(
Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")
);
double[] prices = {4.99, 3.49, 2.99, 5.99, 4.49};
System.out.println("=== Shopping Cart ===\n");
// Method 1: Enhanced for-each (most common)
System.out.println("1. For-each loop:");
for (String item : cart) {
System.out.println(" • " + item);
}
// Method 2: Index-based (when you need position)
System.out.println("\n2. Index-based loop (with prices):");
double total = 0;
for (int i = 0; i < cart.size(); i++) {
System.out.printf(" %d. %-10s $%.2f%n", i+1, cart.get(i), prices[i]);
total += prices[i];
}
System.out.printf(" Total: $%.2f%n", total);
// Method 3: forEach with lambda (Java 8+)
System.out.println("\n3. forEach with lambda:");
cart.forEach(item -> System.out.println(" → " + item));
// Method 4: forEach with method reference
System.out.println("\n4. Method reference:");
cart.forEach(System.out::println);
// Bonus: Iterate with index using forEach
System.out.println("\n5. Indexed forEach (manual counter):");
int[] counter = {0}; // Array trick for lambda
cart.forEach(item -> {
System.out.println(" [" + counter[0] + "] " + item);
counter[0]++;
});
}
}
cart ← [Coffee, Milk, Bread, Eggs, Butter]
4public class IterateAll {5 public static void main(String[] args) {6 ArrayList<String> cart→ [Coffee, Milk, Bread, Eggs, Butter] = new ArrayList<>(7 Arrays.asList("Coffee", "Milk", "Bread", "Eggs", "Butter")8 );9 double[] prices = {4.99, 3.49, 2.99, 5.99, 4.49};10 11 System.out.println("=== Shopping Cart ===\n");12 13 // Method 1: Enhanced for-each (most common) //#?foreach14 System.out.println("1. For-each loop:");15 for (String item : cart) {output=== Shopping Cart === 1. For-each loop:for (String item : cart)
pass 1 of 514System.out.println("1. For-each loop:");15for (String itemCoffee : cart[Coffee, Milk, Bread, Eggs, Butter]) {16 System.out.println(" • " + itemCoffee);17}output • CoffeeAll 5 passes — pass 1 is the card above pass item1 Coffee 2 Milk 3 Bread 4 Eggs 5 Butter total ← 0.0
19// Method 2: Index-based (when you need position) //#?indexed20System.out.println("\n2. Index-based loop (with prices):");21double total→ 0.0 = 0;22for (int i = 0; i < cart.size(); i++) {output 2. Index-based loop (with prices):total ← 4.99
pass 1 of 521double total = 0;22for (int i0 = 0; i < cart.size(); i++) {23 System.out.printf(" %d. %-10s $%.2f%n", i0+1, cart.get(i), prices[i]4.99);24 total→ 4.99 += prices[i]4.99;25}All 5 passes — pass 1 is the card above pass iprices[i]total1 0 4.99 0.0 → 4.99 2 1 3.49 4.99 → 8.48 3 2 2.99 8.48 → 11.47 4 3 5.99 11.47 → 17.46 5 4 4.49 17.46 → 21.950000000000003 System.out.printf(" Total: $%.2f%n", total);
25 }26 System.out.printf(" Total: $%.2f%n", total21.950000000000003);27 28 // Method 3: forEach with lambda (Java 8+) //#?lambda29 System.out.println("\n3. forEach with lambda:");30 cart.forEach(item -> System.out.println(" → " + item));31 32 // Method 4: forEach with method reference33 System.out.println("\n4. Method reference:");34 cart.forEach(System.out::println);35 36 // Bonus: Iterate with index using forEach37 System.out.println("\n5. Indexed forEach (manual counter):");38 int[] counter = {0}; // Array trick for lambda39 cart.forEach(item -> {40 System.out.println(" [" + counter[0] + "] " + item);41 counter[0]++;42 });43}output 3. forEach with lambda: 4. Method reference: 5. Indexed forEach (manual counter):item ->
pass 1 of 538int[] counter = {0}; // Array trick for lambda39cart.forEach(item -> {40 System.out.println(" [" + counter[0] + "] " + item);41 counter[0]++;42});output [0] Coffee
Use for-each: for (String item : cart). Cleaner than index-based loops.
Exercise: ArraylistIntegers.java
Work with ArrayList<Integer> and wrapper classes