ArrayList的Contains方法始终返回false。

huangapple go评论62阅读模式
英文:

ArrayList Contains with Object always returns false

问题

这是我的代码:

package bikeproject;

import java.util.ArrayList;
import java.util.Random;

public class BikerList {
    public static void main(String[] args) {
        ArrayList<Bike> bikes = new ArrayList<Bike>();

        int mountainBikeSales = 0;
        int roadBikeSales = 0;

        fillArray(bikes);

        displayStock(bikes);

//		calculateStock(bikes);
        System.out.println(displayBikeNumbers(bikes));
    }

    static void fillArray(ArrayList<Bike> bikes) {
        Random rand = new Random();
//		int number = 2;
//		int a = rand.nextInt(2);

        for (int i = 0; i < 10; i++) {
            if (rand.nextInt(2) < 1) {
//				System.out.println(rand.nextInt(2));
                bikes.add(new MountainBike());
            } else {
//				System.out.println(rand.nextInt(2));
                bikes.add(new RoadBike());
            }
        }
    }

    static void displayStock(ArrayList<Bike> bikes) {
        for (Bike bike : bikes) {
            System.out.println(bike);
        }
    }

    static int calculateStock(ArrayList<Bike> bikes) {
        int bikesSold = 0;
        for (Bike bike : bikes) {
            if (bike instanceof MountainBike) {
                bikesSold++;
            }
        }
        return bikesSold;
    }

    static String displayBikeNumbers(ArrayList<Bike> bikes) {
        int mb = 0;
        int rb = 0;

        mb = calculateStock(bikes);
        rb = bikes.size() - mb;

        return "\nStock Levels" + "\nWe have " + mb + " Mountain Bikes in Stock" + "\nWe have " + rb
                + " Road Bikes in Stock";
    }
}

我确实相信calculateStock()方法中存在错误,因为它总是返回false,在calculateStock()方法中,我希望如果数组中的每辆自行车都是山地自行车的实例,就将值添加到bikesSold变量中。

由上述代码生成的输出:

Stock Levels
We have 0 Mountain Bikes in Stock
We have 10 Road Bikes in Stock

山地自行车始终为0,尽管它已经在ArrayList中。

预期的输出,类似于这样:

Stock Levels
We have 4 Mountain Bikes in Stock
We have 6 Road Bikes in Stock
英文:

Here is my code:

package bikeproject;
import java.util.ArrayList;
import java.util.Random;
public class BikerList {
public static void main(String[] args) {
ArrayList&lt;Bike&gt; bikes = new ArrayList&lt;Bike&gt;();
int mountainBikeSales = 0;
int roadBikeSales = 0;
fillArray(bikes);
displayStock(bikes);
//		calculateStock(bikes);
System.out.println(displayBkeNumbers(bikes));
}
static void fillArray(ArrayList&lt;Bike&gt; bikes) {
Random rand = new Random();
//		int number = 2;
//		int a = rand.nextInt(2);
for (int i = 0; i &lt; 10; i++) {
if (rand.nextInt(2) &lt; 1) {
//				System.out.println(rand.nextInt(2));
bikes.add(new MountainBike());
} else {
//				System.out.println(rand.nextInt(2));
bikes.add(new RoadBike());
}
}
}
static void displayStock(ArrayList&lt;Bike&gt; bikes) {
for (Bike bike : bikes) {
System.out.println(bike);
}
}
static int calculateStock(ArrayList&lt;Bike&gt; bikes) {
int bikesSold = 0;
for (Bike bike : bikes) {
if (bikes.contains(new MountainBike())) {
bikesSold++;
}
}
return bikesSold;
}
static String displayBkeNumbers(ArrayList&lt;Bike&gt; bikes) {
int mb = 0;
int rb = 0;
mb = calculateStock(bikes);
rb = bikes.size() - mb;
return &quot;\nStock Levels&quot; + &quot;\nWe have &quot; + mb + &quot; mountain Bike in Stock&quot; + &quot;\nWe have &quot; + rb
+ &quot; Road bikes in Stock&quot;;
}
}

I really believe there is an error in the calculateStock() method because it always returns false, In the calculateStock() method I want to add the value to the bikesSold variable if each bike in the array is an instance of a mountain bike.

The output generated by the code above:

Stock Levels
We have 0 mountain Bike in Stock
We have 10 Road bikes in Stock

Mountain Bike is always 0 although it is already in the ArrayList.

The expected output, something like this:

Stock Levels
We have 4 Mountain Bikes in stock
We have 6 Road Bikes in stock

答案1

得分: 5

static int calculateStock(ArrayList<Bike> bikes) {
    int bikesSold = 0;
    for (Bike bike : bikes) {
        if (bike instanceof MountainBike) {
            bikesSold++;
        }
    }
    return bikesSold;
}
英文:
static int calculateStock(ArrayList&lt;Bike&gt; bikes) {
    int bikesSold = 0;
    for (Bike bike : bikes) {
        if (bikes.contains(new MountainBike())) { //problematic line
            bikesSold++;
        }
    }
    return bikesSold;
}

does not work, because, .contains(Object o):
> returns true if and only if this list contains at least one element e such that Objects.equals(o, e).

So, bikes.contains(new MountainBike()) will always return true as long as there is, at least, one instance of the MountainBike contained (according to the .equals(Object o) method) in the bikes list.

Rather, you should want to check if the current object is an instance of a particular type you would want to check it against (MountainBike, in this case), like this:

static int calculateStock(ArrayList&lt;Bike&gt; bikes) {
    int bikesSold = 0;
    for (Bike bike : bikes) {
        if (bike instanceof MountainBike) {
            bikesSold++;
        }
    }
    return bikesSold;
}

答案2

得分: 1

这个 if (bikes.contains(new MountainBike())) 创建一个新对象并检查它是否已经在列表中。事实并非如此。

你需要这样做 if (bike instanceof Mountainbike)

有更好的方法,但这将解决你的问题。

英文:

This if (bikes.contains(new MountainBike())) creates a new object and checks if it's already in the list. It's not.

You need to do something like if (bike instanceof Mountainbike) .

There are better ways, but that will solve your problem.

huangapple
  • 本文由 发表于 2020年10月14日 05:16:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64343274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定