公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了为什么ArrayList CONTAINS()方法不能处理对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java,遇到了一个问题。为什么ArrayList CONTAINS()方法不能处理对象?它似乎可以处理字符串和其他基本数据类型,但不能处理对象。我是否使用了错误的方法?

程序在第8-11行创建数据,并从命令行接收数据。来自命令行的数据为:

avacado 10 .50 broccoli 20 .75 cauliflower 30 1.00 banana 300 .55

命令行上的最后一个条目”banana 3000.55″与第11行上的数据相匹配,应该跳过,因为它已经存在,但CONTAINS()表示它不在那里。怎么会这样?帮助。

以下是我的代码:

package java21days;

import java.util.*;

public class FruitBasket3 {

    ArrayList<Fruit> cart;
    Fruit apple = new Fruit("apple", 100, .75F);
    Fruit orange = new Fruit("orange", 200, .90F);
    Fruit banana = new Fruit("banana", 300, .55F);
    Fruit kumkwat = new Fruit();

    public FruitBasket3(String[] userCodes) {
        cart = new ArrayList<>();
        int quantity;
        float price;

        addCode(apple);
        addCode(orange);
        addCode(banana);
        addCode(kumkwat);

        for (int j = 0; j < userCodes.length; j+=3) {
            quantity = Integer.parseInt(userCodes[j+1]);
            price = Float.parseFloat(userCodes[j+2]);
            addCode(new Fruit(userCodes[j],quantity,price));
        }

        // display all codes
        for (Fruit code : cart) {
            System.out.println("Fruit: " + code.getName() + " Price: " +
                    code.getQuantity() * code.getPrice());
        }
    }

    private void addCode(Fruit inFruit) {
        boolean match = false;
        System.out.println("Inside addCode with fruit: " + inFruit.getName() + 
                " Quantity: " + inFruit.getQuantity() + " Price: " + inFruit.getPrice());
        if (!cart.contains(inFruit)) {
            System.out.println("    Passed the contains test.");
            for (Fruit item : cart) {
                System.out.println("    Existing: " + item.getName() + 
                        " Quantity: " + item.getQuantity() + " Price: " + item.getPrice());
                System.out.println("    New:      " + inFruit.getName() + 
                        " Quantity: " + inFruit.getQuantity() + " Price: " + inFruit.getPrice());

                //if (inFruit.getName() == item.getName() &&
                //        inFruit.getQuantity() == item.getQuantity() &&
                //        inFruit.getPrice() == item.getPrice()){
                if (inFruit.getName().equals(item.getName()) &&
                        inFruit.getQuantity() == item.getQuantity() &&
                        inFruit.getPrice() == item.getPrice()){
                    match = true;
                    System.out.println("        Did not pass the individual element comparision test.");
                } else {
                    System.out.println("        Passed the individual element comparision test.");
                }
            }
            if (!match) {
                System.out.println("            >>>> adding.");
                cart.add(inFruit);
            } else {
                System.out.println("            >>>> did not add.");
            }
        } else {
            System.out.println("    Fruit: " + inFruit.getName() + " already on file ... bypassing.");
        }
    }

    public static void main(String[] arguments) {
        if (arguments.length % 3 != 0) {
            System.out.println("Error!!! Incorrect number of arguments.");
        } else {
            FruitBasket3 keeper = new FruitBasket3(arguments);
        }
    }

}

public class Fruit {
    private String name;
    private int quantity;
    private float price;

    public Fruit() {
        name = "";
        quantity = 0;
        price = 0.0F;
    }

    public Fruit(String inName, int inQuantity, float inPrice) {
        name = inName;
        quantity = inQuantity;
        price = inPrice;
    }

    public String getName(){
        return name;
    }

    public int getQuantity(){
        return quantity;
    }

    public float getPrice(){
        return price;
    }
}

输出为:

run:
Inside addCode with fruit: apple Quantity: 100 Price: 0.75
    Passed the contains test.
            >>>> adding.
Inside addCode with fruit: orange Quantity: 200 Price: 0.9
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      orange Quantity: 200 Price: 0.9
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit: banana Quantity: 300 Price: 0.55
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit:  Quantity: 0 Price: 0.0
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:       Quantity: 0 Price: 0.0
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:       Quantity: 0 Price: 0.0
        Passed the individual element comparision test.
    Existing: banana Quantity: 300 Price: 0.55
    New:       Quantity: 0 Price: 0.0
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit: avacado Quantity: 10 Price: 0.5
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      avacado Quantity: 10 Price: 0.5
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:      avacado Quantity: 10 Price: 0.5
        Passed the individual element comparision test.
    Existing: banana Quantity: 300 Price: 0.55
    New:      avacado Quantity: 10 Price: 0.5
        Passed the individual element comparision test.
    Existing:  Quantity: 0 Price: 0.0
    New:      avacado Quantity: 10 Price: 0.5
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit: broccoli Quantity: 20 Price: 0.75
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      broccoli Quantity: 20 Price: 0.75
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:      broccoli Quantity: 20 Price: 0.75
        Passed the individual element comparision test.
    Existing: banana Quantity: 300 Price: 0.55
    New:      broccoli Quantity: 20 Price: 0.75
        Passed the individual element comparision test.
    Existing:  Quantity: 0 Price: 0.0
    New:      broccoli Quantity: 20 Price: 0.75
        Passed the individual element comparision test.
    Existing: avacado Quantity: 10 Price: 0.5
    New:      broccoli Quantity: 20 Price: 0.75
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit: cauliflower Quantity: 30 Price: 1.0
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
    Existing: banana Quantity: 300 Price: 0.55
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
    Existing:  Quantity: 0 Price: 0.0
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
    Existing: avacado Quantity: 10 Price: 0.5
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
    Existing: broccoli Quantity: 20 Price: 0.75
    New:      cauliflower Quantity: 30 Price: 1.0
        Passed the individual element comparision test.
            >>>> adding.
Inside addCode with fruit: banana Quantity: 300 Price: 0.55
    Passed the contains test.
    Existing: apple Quantity: 100 Price: 0.75
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: orange Quantity: 200 Price: 0.9
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: banana Quantity: 300 Price: 0.55
    New:      banana Quantity: 300 Price: 0.55
        Did not pass the individual element comparision test.
    Existing:  Quantity: 0 Price: 0.0
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: avacado Quantity: 10 Price: 0.5
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: broccoli Quantity: 20 Price: 0.75
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
    Existing: cauliflower Quantity: 30 Price: 1.0
    New:      banana Quantity: 300 Price: 0.55
        Passed the individual element comparision test.
            >>>> did not add.***
Fruit: apple Price: 75.0
Fruit: orange Price: 180.0
Fruit: banana Price: 165.0
Fruit:  Price: 0.0
Fruit: avacado Price: 5.0
Fruit: broccoli Price: 15.0
Fruit: cauliflower Price: 30.0
BUILD SUCCESSFUL (total time: 0 seconds)

推荐答案

它不适用于您的类,因为它没有有效的public boolean equals(...)方法重写。给它一个,一个使用关键字字段来测试功能相等性并包含将工作的字段。

根据contains method section of the ArrayList API:

如果此列表包含指定元素,则返回TRUE。更正式地说,当且仅当此列表包含至少一个元素e且(o==NULL?)时,才返回TRUE。E==空:o.equals(E))。

仅将名称字段用于相等测试,并使其成为final字段,因为您不希望使用可能更改的字段。还应为其提供hashCode重写,该重写也使用在equals方法中测试的相同字段。

类似:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Fruit other = (Fruit) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

可以工作

这篇关于为什么ArrayList CONTAINS()方法不能处理对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:ArrayList 对象 方法
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定