当前位置: 首页 > 知识库问答 >
问题:

理解数组列表和对象

钦高峯
2023-03-14
public class Product {
    // Variables.
    private String name;      // Name  
    private Double price;     // Price

    Product() {}    // Default constructor with no parameters.

    Product(String name, Double price) {    // Constructor with parameters.
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Double getPrice() {
        return price;
    }
    public void setPrice(Double price) {
        this.price= price;
    }
    public String toString() {    // Overriding "toString()".
        return "\nName: " + this.name + "\nPrice: " + this.price;
    }


public boolean equals(Object obj) {    // Overriding equals()
   if(this == obj) {
      return true;
   }
   if(obj == null || obj.getClass() != this.getClass()) {
      return false;
   }
   Product product = (Product) obj;
   return this.name.equals(product.name)&& this.price.equals(product.price);
}
    }
import java.util.*;
    import java.io.*;
    public class Main {
        private static BufferedReader r = new BufferedReader (new InputStreamReader(System.in));
        private static String readln() throws IOException{
            return r.readLine();
        }
        private static long readInput() throws IOException{    // Use this to read input for the menu options.
            return Integer.valueOf(readln());
        }
        public static void menu(){    // Menu
            System.out.println("-------------------------" +
                    "\nAdd new product(1)" +
                    "\nSearch for product(2)" +
                    "\nDelete product(3)" +
                    "\nShow all products(4)" +
                    "\nReturn the number of products(5)" +
                    "\nExit(-1)" +
                    "\n-------------------------");
        }

        public static void main (String args[]) throws IOException{
            // This is the ArrayList for the "Product".
            ArrayList<Product> products = new ArrayList<Product>();
            int option = 0;
            do {
                menu();
                option = (int)readInput();
                switch (option){
                    case 1:{
                        System.out.println("Insert product name: ");
                        String name= readln();
                        System.out.println("Insert product price: ");
                        Double price = Double.parseDouble(readln());
                        products.add(new Product(name, price));
                        break;
                    }
                    case 2:{
                        System.out.println("Insert product name: ");
                        String name= readln();
                        System.out.println("Insert product price: ");
                        Double price= Double.parseDouble(readln());
                        if ((products.contains(new Product (name, price)))){
                            System.out.println("Works!");
                        }

                        break;
                    }
                    case 3:{

                        break;
                    }
                    case 4:{

                        break;
                    }
                    case 5:{
                        System.out.println("Number of products: " + products.size());
    //This prints with no problems, therefor the objects DO exist in the ArrayList.
                        break;
                    }
                }
            }while((option > 0) && (option < 6));
        }
    }

编辑:对于equals()的最后一行,还可以放入:

if( (this.price.equals(product.getPrice())) && (this.name.equals(product.getName())) ) {
            return true;
        }

共有1个答案

欧阳声
2023-03-14

要使其工作,您还应该将equals方法重写为people对象内的compere字段,重写equals方法覆盖equals方法

参数化构造函数中可能存在一个bug。它看起来应该是:

Product(final String name, final Double price) {    // Constructor with parameters.
    this.name = name;
    this.price = price;
}

最后一个字阻止我们改变输入参数的值。

@Override
public boolean equals(Object obj) {    // Overriding "equals()".
    // at first check if objects are the same -> your code
    if (this == obj) {
        return true;
    }

    // secondly we chack if objects are instances of the same class if not return false
    if (obj != null && this.getClass() != obj.getClass()) {
        return false;
    }

    // then compare objects fields. If fields have the same values we can say that objects are equal.
    Product product = (Product) obj;
    return this.name.equals(product.name) && this.price.equals(product.price);
}

而不是做

products.contains(name, price);

试试看

products.contains(new Product(name, price))

要从列表中删除元素,首先可以找到元素的索引和use remove方法。

products.remove(products.indexOf(new Product(name, price)))
 类似资料:
  • 我有 和Xlink类,该类在内联变量中工作良好。 对于的XML输入 并且子课程拒绝被取消注册(没有任何异常被抛出)。 注:不幸的是,MOXy不是一个选项。 编辑:新编组对象 Edit2:在对测试对象进行了一些解组和编组的实验之后,我发现我需要在内容的标题中定义xmlns名称空间,以便与类似问题在于,我从一个包装类中获取Course元素,该包装类由resteasy解析出来。因此,生成的类不会携带名称

  • 我是Java和Stack Overflow的新手,我有一个关于排列的问题。 方法:我使用中的对象生成。每个的大小从(可能最小为1)到,并包含具有唯一名称属性的自定义生成对象。 问题:现在我的问题是如何在我的外部(y轴)中获得从第一个到最后一个的所有可能对象组合的排列(我想我们可以说这是x轴)? 我试着举一个简单的例子: : 1.1|1.2|1.3 : 2.1 : 3.1|3.2 这里,这些位于外部

  • 我有以下JSON文件要反序列化

  • 问题内容: 我如何用对象填充ArrayList,而内部的每个对象都不相同? 问题答案:

  • 问题内容: 我已经进行了很多编程工作,最近开始学习更多纯粹的计算机科学主题(用于工作面试)。 我知道Array和LinkedList数据结构之间的区别,但是现在我已经开始使用Java,我看到了这个ArrayList,在概念上我遇到了麻烦。 网络搜索仅真正向我展示了如何使用它们以及何时使用它们(每种优点),但是没有任何东西可以回答我的问题: 什么是ArrayList?我的假设是,它是一个维护对每个元

  • 我有一个类来代表一个玩家的手牌。但是,我(在另一个类中)有一个数组列表,我想在其中表示一堆玩家手。问题是我不知道如何在多手牌的数组列表中将一张牌添加到手牌中。我有一个代表卡牌和一副牌的类,效果很好。我只是试图了解如何将对象添加到数组列表中的对象。谢谢!