Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 添加到数组列表中_Java_Arraylist - Fatal编程技术网

Java 添加到数组列表中

Java 添加到数组列表中,java,arraylist,Java,Arraylist,我是Java新手。。但我相信我能以更有效的方式完成这项工作 此方法的目的是添加具有唯一Id的产品。如果我将产品作为副本添加,则会引发异常。这个序列不适用于多线程环境 public void addProduct(Product product) throws ProductAlreadyExistsException { product.id = ++nextId; this.id = product.id; Iterator<Product>

我是Java新手。。但我相信我能以更有效的方式完成这项工作

此方法的目的是添加具有唯一Id的产品。如果我将产品作为副本添加,则会引发异常。这个序列不适用于多线程环境

public void addProduct(Product product)
        throws ProductAlreadyExistsException {
    product.id = ++nextId;
    this.id = product.id;

    Iterator<Product> it = allProducts.iterator();
    Product p= null;

    boolean flag= false;
    if (!allProducts.isEmpty()) {
        while(it.hasNext()){
            p= it.next();
            if ( p.getName() == product.getName())
                throw new ProductAlreadyExistsException(p.getName());
            else
                flag = true;
        }

    }
    else
        allProducts.add(product.id-1, product);

    if(flag){
        allProducts.add(product.id-1, product);
    }
}
这是行不通的。
感谢您的指导。

一般来说,不能保证任何类型的列表只包含唯一的元素,但我认为您不必经历创建迭代器的过程

仅使用就足够了-如果列表不包含元素,请将其添加进来,否则,抛出异常*

public void addProduct(Product theProduct) throws ProductAlreadyExistsException {
    if(allProducts.contains(theProduct)) {
        throw new ProductAlreadyExistsException("Not unique!");
    }
    allProducts.add(theProduct);
}

*:在我看来,抛出异常有点愚蠢。这应该只用于真正异常的行为。您最好使用某种类型的集合。

一般来说,不能保证任何类型的列表只包含唯一的元素,但我认为您不必经历创建迭代器的过程

仅使用就足够了-如果列表不包含元素,请将其添加进来,否则,抛出异常*

public void addProduct(Product theProduct) throws ProductAlreadyExistsException {
    if(allProducts.contains(theProduct)) {
        throw new ProductAlreadyExistsException("Not unique!");
    }
    allProducts.add(theProduct);
}
*:在我看来,抛出异常有点愚蠢。这应该只用于真正异常的行为。您最好使用某种类型的字符串集。

在Java中,您可以使用s1.equals2方法来确定两个字符串是否相等

 if ( p.getName() == product.getName()) // This will always return false, because references are compared here
你应该做的是:

if ( p.getName().equals(product.getName()) )
注意:我假设getName返回一个字符串。

在Java中,使用s1.equals2方法来标识两个字符串是否相等

 if ( p.getName() == product.getName()) // This will always return false, because references are compared here
你应该做的是:

if ( p.getName().equals(product.getName()) )

注意:我假设getName返回一个字符串。

Makoto的答案是一种确保对象在列表中唯一的简单方法

此外,您需要确保在要添加到列表的对象类中实现equalsObject obj方法。List.contains方法对其包含的每个对象调用EqualYourObject,并在EqualYourObject对列表中的任何对象返回true时立即返回true

在这里,您可以看到equalsObject obj的良好实现,可以在产品类中使用


Makoto的答案是一种确保对象在列表中唯一的简单方法

此外,您需要确保在要添加到列表的对象类中实现equalsObject obj方法。List.contains方法对其包含的每个对象调用EqualYourObject,并在EqualYourObject对列表中的任何对象返回true时立即返回true

在这里,您可以看到equalsObject obj的良好实现,可以在产品类中使用


如果知道没有重复项,那么集合的查找效率也会更高。不幸的是,id没有被创建。。Id为空。。我应该更改构造函数吗。我在构造函数中有以下代码。public ProductString name,double price,DeptCode代码{thisnull,name,price,code;}public ProductInteger id,String name,double price,DeptCode代码{this.id=id;this.name=name;this.price=price;this.dept=code;该id是否会绑定到产品对象?如果知道没有重复项,则集合的查找效率也会更高。不幸的是,该id没有被创建。id为null。我是否应该更改构造函数。我在constructor.public ProductString name,double price,DeptCode代码{thisnull,name,price,code;}public ProductInteger id,String name,double price,DeptCode代码{this.id=id;this.name=name;this.price=price;this.dept=code;id是否会绑定到Product对象?最好在Product中实现equals方法,这样他们就可以将自己与其他对象进行比较。执行此操作时,还记得实现hashCode。IDE可以自动为您生成这两个方法。关于equals和hashCode契约,请阅读。我做了上述更改,效果更好。感谢洛蒂特在产品中实现equals方法更好,这样他们可以将自己与其他对象进行比较。在这样做的同时,还记得实现hashCode。IDE可以自动为您生成这两个方法。关于equals和d hashCode合同,请阅读。我做了上述更改,效果更好。非常感谢