I am trying to add to an arraylist of arraylists. I have an arraylist called prod and 2d arraylist called shoppingBasket.
The main issue I have is that I want it to add several items to the basket (ie several prod lists to the list shoppingBasket) but instead of doing that it replaces the first item of the basket with the next (so there is only every one item in the basket). I am fairly new to java and am not sure how to correct this.
I have also tried making another class 'item' then create objects from here then add to the arraylist as ArrayList e.g
public class Item {
private int bar;
private String name;
private String type;
private String brand;
private String colour;
private String con;
private int quantity;
private float cost;
private String addi;
public Item (int bar, String name, String type, String brand, String colour, String con, int quantity,float cost, String addi) {
this.bar = bar;
this.name = name;
this.type = type;
this.brand = brand;
this.colour = colour;
this.con = con;
this.quantity = quantity;
this.cost = cost;
this.addi = addi;
}
public class Basket {
//arraylist for the shopping basket
private ArrayList<Item> shoppingBasket;
public Basket() {
shoppingBasket = new ArrayList<>();
}
//function adding items to the basket
public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {
Item items = new Item (bar, name, type, brand, colour, con, quantity, cost, addi);
shoppingBasket.add(items);
}
}
When I tried this method anad printed the arraylist to test, i would just show something like [Item@23455] and would still replace the item originally there instead of adding, plus i don't really understand how to do this properly so if anyone could explain that method to me (if it is easier to use than what i have already done) it would be greatly appreciated. Although I also would prefer if I didn't have to change my code too much.
The code in question: Function in one class that adds an item to the basket.
public class Basket {
//arraylist for the shopping basket
private ArrayList<ArrayList<String>> shoppingBasket;
public Basket() {
shoppingBasket = new ArrayList<>();
}
//function adding items to the basket
public void addToBasket(int bar, String name, String type, String brand, String colour, String con, int quantity, float cost, String addi) {
ArrayList<String> prod = new ArrayList<>();
//adding one item to the basket shop.add(bar); shop.add(name);
prod.add(Integer.toString(bar));
prod.add(name);
prod.add(type);
prod.add(brand);
prod.add(colour);
prod.add(con);
prod.add(Integer.toString(quantity));
prod.add(Float.toString(cost));
prod.add(addi);
shoppingBasket.add(prod);
System.out.println(shoppingBasket);
}
n.b. the items are being added from a table using a listener in another table (each cell is a different variable):
selectionModel.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
int row = tableViewAll.getSelectedRow();
viewAllModel=(DefaultTableModel) tableViewAll.getModel();
int bar=Integer.parseInt(viewAllModel.getValueAt(row,0).toString());
String name = viewAllModel.getValueAt(row,1).toString();
String type = viewAllModel.getValueAt(row,2).toString();
String brand = viewAllModel.getValueAt(row,3).toString();
String colour= viewAllModel.getValueAt(row,4).toString();
String con = viewAllModel.getValueAt(row,5).toString();
int quantity=1;
float cost=Float.parseFloat(viewAllModel.getValueAt(row,7).toString());
String addi= viewAllModel.getValueAt(row,8).toString();
Basket item = new Basket();
item.addToBasket(bar, name, type, brand, colour, con, quantity, cost, addi);
Thanks in advance :)