自定义LinkedList条件筛选实现求助(仅允许修改Main.java)
Alright, let's fix those condition checks in your Main.java file. Since you already know how to traverse the custom LinkedList, we'll build right on top of that logic to meet both requirements. Here's how to do it:
Step 1: Lay the Traversal Groundwork
First, make sure your traversal loop is set up correctly (you mentioned you can already print entries, so this should feel familiar):
Node current = linkedList.getHead(); // Assume your LinkedList has a getHead() method to access the first node while (current != null) { Smartphone phone = (Smartphone) current.getData(); // Cast the node's data to Smartphone type // Your condition checks go here current = current.getNext(); // Move to the next node in the list }
Step 2: Implement Requirement 1 - Filter Nokia Phones ≥ $1200
We'll create a collection to store qualifying Nokia phones (you can use an ArrayList or even another instance of your custom LinkedList if preferred). Add this logic inside your traversal loop:
// Initialize storage for qualifying Nokia phones before the loop List<Smartphone> qualifyingNokias = new ArrayList<>(); // Inside the traversal loop: if ("Nokia".equals(phone.getBrand()) && phone.getPrice() >= 1200) { qualifyingNokias.add(phone); // If you want to print the info immediately instead of storing: System.out.println("Qualifying Nokia: Model - " + phone.getModel() + ", Price - $" + phone.getPrice()); }
Pro tip: Always use
string.equals()instead of==for string comparisons—this checks actual text content, not object references, which avoids bugs.
Step 3: Implement Requirement 2 - Count Samsung Galaxy Y Phones
Initialize a counter variable before the traversal loop, then increment it whenever we match the target phone:
// Initialize counter before the loop int galaxyYCount = 0; // Inside the traversal loop: if ("Samsung".equals(phone.getBrand()) && "Galaxy Y".equals(phone.getModel())) { galaxyYCount++; } // After the loop, output the final count System.out.println("Total Samsung Galaxy Y phones: " + galaxyYCount);
Full Integrated Code for //The conditions Section
Here's how all the logic fits together in your Main.java file:
// The conditions Node current = linkedList.getHead(); List<Smartphone> qualifyingNokias = new ArrayList<>(); int galaxyYCount = 0; while (current != null) { Smartphone phone = (Smartphone) current.getData(); // Check for Nokia phones meeting the price requirement if ("Nokia".equals(phone.getBrand()) && phone.getPrice() >= 1200) { qualifyingNokias.add(phone); System.out.println("Nokia Phone (≥$1200): " + phone.getModel() + " | $" + phone.getPrice()); } // Count Samsung Galaxy Y units if ("Samsung".equals(phone.getBrand()) && "Galaxy Y".equals(phone.getModel())) { galaxyYCount++; } current = current.getNext(); } // Final output for the count System.out.println("\nTotal Samsung Galaxy Y phones found: " + galaxyYCount); // Use the qualifyingNokias list later if needed (e.g., print all stored entries)
Key Reminders:
- Type Casting: We cast
current.getData()toSmartphonebecause your customNodeclass likely stores genericObjectdata. - No Forbidden Modifications: All logic lives strictly in
Main.java—we don't touch theSmartphone,LinkedList, orNodeclasses as required. - Flexibility: If you can't use
ArrayList, replace it with an instance of your customLinkedListto store the filtered Nokia phones.
内容的提问来源于stack exchange,提问作者Zach Unlimited

