You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义LinkedList条件筛选实现求助(仅允许修改Main.java)

Solution for Your Custom LinkedList Filtering & Counting Tasks

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() to Smartphone because your custom Node class likely stores generic Object data.
  • No Forbidden Modifications: All logic lives strictly in Main.java—we don't touch the Smartphone, LinkedList, or Node classes as required.
  • Flexibility: If you can't use ArrayList, replace it with an instance of your custom LinkedList to store the filtered Nokia phones.

内容的提问来源于stack exchange,提问作者Zach Unlimited

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 19:52:42