简体   繁体   English

关于排序数组列表

[英]About sorting array list

I am trying to ensure that the objects i insert in productDatabase have not been already inserted and that i sort my arraylist using method sortData but without using any comparators in method sortData 我正在尝试确保我在productDatabase中插入的对象尚未插入,并且我使用方法sortData对我的arraylist进行排序,但不使用方法sortData中的任何比较器

public class Application {

public static void main(String[] args) {

    Product  p = new Product(15,"test",3.45);
    Product  p2 = new Product(15,"test",3.45);
    Product  p3 = new Product(4716,"koukouroukou",1.25);
    Product  p4 = new Product(6002,"bananofatsoula",0.60);

    ProductDatabase productDatabase = new ProductDatabase();

    productDatabase.addProduct(p);
    productDatabase.addProduct(p2);
    productDatabase.addProduct(p3);
    productDatabase.addProduct(p4);

    productDatabase.printDatabase();

    productDatabase.sortDatabase();
    productDatabase.printDatabase();

}
public class Product {

    private int code;
    private String name;
    private double price;

    public Product(int code, String name, double price){
        this.code = code;
        this.name = name;
        this.price = price;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    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(){
        return code+" , description: "+name+", price: "+price;
    }

    public int hashCode(){
        return 31 * code + name.hashCode();
    }

    public boolean equals(Object o){
        Product other = (Product)o;
    if (this.code == other.code){
        return true;
    }
    else{ 
        return false;
    }
}
import java.util.ArrayList;

import java.util.Collection;


public class ProductDatabase {

private ArrayList<Product> productDatabase;

public ProductDatabase(){
    productDatabase = new ArrayList<Product>();
}

public void addProduct(Product p){
    if(!productDatabase.contains(p)){
        productDatabase.add(p);
    }
}

public void printDatabase(){
    for(Product product : productDatabase){
        System.out.println(product);
    }
}

public void sortDatabase(){

// ????????????????????????????????????????????????????????????

So my questions are 所以我的问题是

  1. Does contain(p) is enough to ensure that the same product is not already in the list? 包含(p)是否足以确保相同的产品不在列表中?
  2. products are the same when they have the same code and name.if not what i have to do? 当它们具有相同的代码和名称时,产品是相同的。如果不是我必须做的事情?
  3. How i sort my withous using comparators in class ProductDatabase.maybe by a new method in product ? 我如何使用ProductDatabase.maybe类中的比较器通过产品中的新方法对我的toous进行排序?
  4. Does productDatabase extends Product??? productDatabase是否扩展了Product ???

You can return bool value in order to know the product is already there or not . 您可以返回bool值以了解产品是否已存在。 Code is used to ensure the differentiation of products if not add another code id. 如果不添加其他代码ID,代码用于确保产品的差异化。 Product instance will carry information of just one Product so Sort must be done in the member function of class having all the records of product, not just one. 产品实例将只包含一个产品的信息,因此必须在具有产品所有记录的类的成员函数中进行排序,而不仅仅是一个。 No product_database does not extend product . 没有product_database不扩展产品。 It is a log of product class not a part . 它是产品类别的日志而不是其中的一部分。

your questions 1 and 2 are a little unclear. 你的问题1和2有点不清楚。 Can you re-write them? 你能重新写一下吗? As for question 3. No ProductDatabase does not extend product, neither should it. 至于问题3.没有ProductDatabase不扩展产品,也不应该扩展产品。 ProductDatabase HAS products. ProductDatabase HAS产品。 ProductDatabase is not a product ProductDatabase 不是产品

  1. Yes, contains() is enough (in our case) to ensure uniqueness 是的, contains()就足够了(在我们的例子中)以确保唯一性
  2. Since you implemented equals and hashCode - you're good 既然你实现了equalshashCode - 你很好
  3. You don't need to sort if you don't have another purpose to do so, but since you're using an ArrayList every time contains() is called it iterates the whole list which is not very efficient. 如果没有其他目的,则无需进行sort ,但由于每次调用contains()时都使用ArrayList,因此迭代整个列表并不是非常有效。 A better implementation would use Set (a HashSet for example) 更好的实现将使用Set (例如HashSet
  4. ProductDatabase does not have to extend Product - it contains a list/set of products but it doesn't have any character/behavior like Product ProductDatabase不必扩展Product - 它包含一个列表/一组产品,但它没有像Product这样的任何字符/行为
  1. Yes, contain(p) is enough to ensure that the same product is not already in the list, because you overrided "equals" method. 是的,包含(p)足以确保相同的产品不在列表中,因为您覆盖了“等于”方法。 In "equals" you can use shorter construction: 在“等于”中,您可以使用更短的结构:

     Product other = (Product)o; return this.code == other.code; 
  2. For sort ArrayList with java.util.Collections class two options possible: 对于带有java.util.Collections类的sortList,可以选择两个选项:

    • Collections.sort(List list, Comparator c). Collections.sort(列表列表,比较器c)。 You have to write own Comparator class and pass as second parameter. 您必须编写自己的Comparator类并作为第二个参数传递。

    • Collections.sort(List list) and class Product must implement Comparable interface Collections.sort(列表列表)和类Product必须实现Comparable接口

Yes, Contain(p) is enough to ensure that the same product is not already in the list BUT that is NOT efficient. 是的,包含(p)足以确保相同的产品不在列表中但是效率不高。 Use a Set instead of ArrayList. 使用Set而不是ArrayList。

For question 2, you have to decide the when the two products are equal and code that in your equals method like you did for 'product code' 对于问题2,您必须决定两个产品何时相等,并在您的equals方法中编写代码,就像您对“产品代码”所做的那样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM