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

如何通过regNum更新ArrayList中Meter对象的workOrNot字段值?

Fixing the doesNotWork Method in MeterArchive

Hey there! Let's get that doesNotWork method sorted out— I spot the two issues tripping you up right away. Let's break them down and fix the code step by step.

The Two Key Issues

  • Wrong string comparison: Using == to check regNum values compares object references, not the actual text content. In Java, you need equals() (or equalsIgnoreCase() for case-insensitive checks) to compare what the strings actually say.
  • Misusing ArrayList.set(): You don't need to replace the entire Meter object in the list. Since Meter is a reference type, once you find the matching object, you can directly update its workOrNot field—this change will automatically show up in the ArrayList, no set() required.

Correct Implementation

Here's how to rewrite the method properly, assuming your Meter class has getter/setter methods for regNum and workOrNot:

public class MeterArchive {
    private ArrayList<Meter> meters;

    // Your existing constructor and other methods here...

    public void doesNotWork(String regNum) {
        // Loop through each meter in the archive
        for (Meter meter : meters) {
            // Use equals() to compare registration numbers correctly
            if (meter.getRegNum().equals(regNum)) {
                // Directly update the workOrNot field of the matching meter
                meter.setWorkOrNot("No");
                // If regNum is unique, we can exit the loop early to save time
                return;
            }
        }
        // Optional: Handle the case where no matching meter is found
        System.err.println("No meter found with registration number: " + regNum);
    }
}

Quick Breakdown of the Fixes

  1. String Comparison Done Right:
    meter.getRegNum().equals(regNum) checks if the two strings have the same content, not just if they're the same object in memory. If your system allows case variations (like "XYZ789" vs "xyz789"), swap in equalsIgnoreCase() instead for more flexibility:

    if (meter.getRegNum().equalsIgnoreCase(regNum)) {
        // ... update the field ...
    }
    
  2. No Need for ArrayList.set():
    Since Meter is a mutable object, when you pull it from the ArrayList and call meter.setWorkOrNot("No"), you're modifying the exact same object that's stored in the list. There's no need to replace it—your change is already reflected in the archive.

For Context: When Would You Use ArrayList.set()?

Just to clarify, if you ever did need to replace the entire Meter object (which isn't necessary here), you'd do something like this—but it's redundant for your use case:

public void doesNotWorkAlternative(String regNum) {
    for (int i = 0; i < meters.size(); i++) {
        Meter meter = meters.get(i);
        if (meter.getRegNum().equals(regNum)) {
            meter.setWorkOrNot("No");
            // Unnecessary here, but this is how you'd replace the object in the list
            meters.set(i, meter);
            return;
        }
    }
}

Skip that extra set() call for your scenario—you're just doing extra work that doesn't add value.

内容的提问来源于stack exchange,提问作者Anders Lund

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:25:56