【问题标题】:Java comparing two strings doesnt seem to workJava比较两个字符串似乎不起作用
【发布时间】:2011-10-31 10:49:00
【问题描述】:

我得到了这个函数,它是一个private boolean 函数,它检查车库内是否已经有一辆相同大小的汽车。如果没有,我将它添加到arrayList,我之前创建了一个printList() 类型函数来遍历arraylist 并打印出值(完美运行)但不知何故我的private boolean 函数没有似乎完全有效。

这是我的代码:

public class Cars {

    public Cars (String size, boolean booking) {
        this.carSize = size;
        this.isBooked = booking;
    }

    public String getSize() {
        return this.carSize;
    }

    public boolean checkBook () {
        return this.isBooked;
    }

    private String carSize;
    private boolean isBooked;

}

public class Locations {

    public Locations (String curLocation) {
        garage = new ArrayList<Cars>();
        location = curLocation;
    }

    public void addCar (String size, boolean booking) {
        if (garage.isEmpty() || !checkCar(size)) {
            garage.add(new Cars(size, booking));
            System.out.println("Car assigned " + location + " " + size);
        }
    }

    private boolean checkCar (String size) {
        for (Cars car : garage) {
            System.out.println("hey");
            if (size.equals(car.getSize())) return true;
        }
        return false; 
    }

    private ArrayList <Cars> garage;
    private String location;

}

输入如下:

Car small City
Car small Redfern
Car small Redfern

输出:

Car assigned City small
Car assigned Redfern small
Car assigned Redfern small

它不应该打印出第二个 Redfern small,因为列表中已经有那个尺寸的汽车了。

【问题讨论】:

  • 顺便说一句,Cars 类不应该是复数。
  • 没有看到你的主要方法很难说它有什么问题。也许你在第三行有多余的空格?你能在相等比较之前修剪它吗?

标签: java arraylist variable-assignment


【解决方案1】:

(我之前的回答是错误的——我匆忙中误读了代码……)

对于发生的事情,我只能想到一种解释:

您已致电new Locations("Redfern") 两次。

这可以解释为什么您会看到两次消息 Car assigned Redfern small,以及为什么您没有看到 hey

您可以通过在Locations 构造函数中放置跟踪图来确认这一点...


这是由size 字符串之一上的前导/尾随空格引起的理论不成立。如果这是问题所在,OP 会将 hey 视为 checkCar 方法迭代了 garage 列表。

【讨论】:

    【解决方案2】:

    如果我以下列方式使用您的代码:

    public class Main
    {
        public static void main( String[] args )
        {
            Locations locationsCity = new Locations( "City" );
            locationsCity.addCar( "small", true );
            Locations locationsRedfern = new Locations( "Redfern" );
            locationsRedfern.addCar( "small", true );
            locationsRedfern.addCar( "small", true );
        }
    }
    

    这是我得到的输出:

    Car assigned City small
    Car assigned Redfern small
    hey
    

    根据您的代码,这似乎很好。

    【讨论】:

    • 我相信 OP 可能会在他们的示例中为每一行输入创建一个全新的 Locations 对象。这可以解释他们显示的输出。
    • 我也认为:Locations locationsRedfern1 = new Locations( "Redfern" ); locationsRedfern1.addCar( "small", true ); Locations locationsRedfern2 = new Locations( "Redfern" ); locationsRedfern2.addCar( "small", true ); 会得到他得到的结果。
    • 嗯,我每次都在制作一个新的位置对象时可能就是这种情况
    【解决方案3】:

    考虑将你的汽车放在一个集合中并实现 equals() 以使用 String.equals() 方法检查大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 2013-02-06
      • 2011-09-20
      • 2015-01-12
      • 2012-12-06
      相关资源
      最近更新 更多