before I added the product to the non-empty list, I tried to validate if the product id and name of the product already existed in the list by going through the list and if there is no such id and name in the list then added the product to the list, but, I got this error #ConcurrentModificationException
-thanks for advance
import java.util.ArrayList;
import java.util.Iterator;
public class ProductList implements Iterator<Product> {
private ArrayList<Product> productListArray = new ArrayList<>();
/* Singleton Design begin */
private static ProductList productListSingleton;
private ProductList() {
}// end of private constructor
public static ProductList instance() {
if (productListSingleton == null) {
productListSingleton = new ProductList();
}
return productListSingleton;
}
/* Singleton Design Phase end */
/*
* method add product into list
*
* @param Product Type
*/
public void addProduct(Product product) {
// if the list is empty just add the product into the list
if (productListArray.isEmpty()) {
productListArray.add(product);
}
/*
* add product only if the id and name not existed in the productList
*/
else {
for (Product product1 : productListArray) {
if (product1.getProductId() == product.getProductId()) {
System.out.println(" the product is already in the list");
}
else if (product1.getProductName().equals(product.getProductName())) {
System.out.println("choose another name for product");
}
else {
productListArray.add(product);
}
}
}
} // end of addProductMethod
/*
* remove the product from the ProductList Parameter product id
*/
public void remove(int productId) {
Iterator<Product> iteratorList = productListArray.iterator();
while (iteratorList.hasNext()) {
Product product = iteratorList.next();
if (product.getProductId() == productId)
iteratorList.remove();
} // end of while
} // end of remove method
/* retrieve the productlist */
public void retrieve() {
Iterator<Product> iteratorList = productListArray.iterator();
while (iteratorList.hasNext()) {
System.out.println(iteratorList.next());
}
}
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
if (next() == null) {
return false;
}
else {
return true;
}
}
@Override
public Product next() {
// TODO Auto-generated method stub
return next();
}
}
From the code in your question:
It is recommended to code to the interface and not to the implementation. Hence that line should be:
You didn't post the code for class
Product
. In the below code, I added it as a record.I don't understand the need to make class
ProductList
a singleton but since it is, note the different ways to write a thread-safe singleton class.I also don't understand why class
ProductList
implements interface Iterator. The implementation is wrong and you don't use it anyway, so I just removed it in the below code.When you ask for help in finding out why your code is throwing an exception it is usually a good idea to post the stack trace of that exception.
Your actual problem is that you are trying to modify
productListArray
while you are iterating over it. Note that the same problem exists in methodremove
.In the below code I also added a
main
method so that you can copy the code and run it.Here is the output I get when I run the above code.