So I decided to use ArrayList but now I want to change it to Array. I'm trying to see if this works with Array only instead of ArrayList. Can anyone sugest some way to do it nice and simple? I want to change it but I searched the internet for some way to do it but the information I found it didn't work for my case.
public class ProductManager {
private List<Product> listOfProduct;
public ProductManager() {
this.listOfProduct= new ArrayList<>();
}
public int addProduct(Product p) {
this.listOfProduct.add(p);
return count();
}
public int count() {
return this.listOfProduct.size();
}
public Product getProduct(int index) {
if (index < 0 || index >=count()) {
return null;
}
return this.listOfProduct.get(index);
}
public boolean removeProduct(int id) {
int index = -1;
for (int i = 0, n = count(); i < n;i++) {
if(this.listOfProduct.get(i).getId() == id) {
index = i;
break;
}
}
if(index != -1) {
this.listOfProduct.remove(index);
return true;
}
return false;
}
Some suggestions are appreciated :) Thank you!