What you are doing is reading over the list and adding each element back. Try this instead. get a new element as method argument and add to the list
public void addStock(StockRecord stock) {
myList.add(stock);
}
Pergunta
I'm writing a class and I need to write a class where the arraylist accept and stores an object of its type. I know this is probably extremely simple but I'm running on no sleep. Here is my Stock Manager class
public class StockManager {
private String appleStocks;
private ArrayList<StockRecord> myList;
public StockManager(String appleStocks) {
this.appleStocks = appleStocks;
this.myList = new ArrayList<StockRecord>();
}
public void addStock() {
for (StockRecord aRecord : this.myList) {
this.myList.add(aRecord);
}
}
If you need to see my other class I can supply it but I don't think it would be necessary
Solução
What you are doing is reading over the list and adding each element back. Try this instead. get a new element as method argument and add to the list
public void addStock(StockRecord stock) {
myList.add(stock);
}
Outras dicas
public void addStock(StockRecord record)
{
if(record != null)
{
this.myList.add(record);
}
}
Just pass a stock record to the class and add it from the inside. If it's null then it will skip the if statement. Easy.