I have this code, and add
is marked red with error:
"Cannot resolve method 'add(java.util.ArrayList<com.example.InventoryItems>)'"
public class Player {
private String name;
private int lives;
private int score;
private int level;
private Weapon weapon;
private ArrayList<InventoryItems> inventoryItems;
public Player(String name) {
this.name = name;
this.lives = 3;
this.score = 0;
this.level = 1;
inventoryItems = new ArrayList<InventoryItems>();
setDefaultWeapon();
}
public void setDefaultWeapon() {
this.weapon = new Weapon("Dagger", 3, WeaponType.Wooden);
}
public void setDefaultInventoryItems() {
InventoryItems rubyNecklace = new InventoryItems("Ruby Necklace", ItemType.Amulet);
rubyNecklace.add(inventoryItems);
}
This is InventoryItems.java
package com.example;
import java.util.ArrayList;
/**
* Created by Pawel on 1/21/16.
*/
enum ItemType { Armor, Ring, Amulet, Junk, Weapon }
public class InventoryItems {
private String name;
private ItemType itemtype;
public InventoryItems(String name, com.example.ItemType itemtype) {
super();
this.name = name;
this.itemtype = itemtype;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public com.example.ItemType getItemtype() {
return itemtype;
}
public void setItemtype(com.example.ItemType itemtype) {
this.itemtype = itemtype;
}
}
Should I add the add
method to InventoryItems?
Why isn't it just possible to add rubyNecklace
to ArrayList
?
I think you got it all mixed up. Change
rubyNecklace.add(inventoryItems);
to
inventoryItems.add(rubyNecklace);
inventoryItems
is the ArrayList
, & rubyNecklace
is the item to be added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With