I'm new to Java and am trying to learn how to add an item to an ArrayList<Object>
. The way that I'm using .add
doesn't seem to work for this. Is there an alternative?
public static void makMat(ArrayList<Maker> makers, ArrayList<Material> materials, int i)
{
assert i >= 50;
for (int makId = 5; makId <= i; makId++)
{
for (Maker maker : makers)
{
//do stuff
}
makers.add(makId); // throws error
makers.get(makers.size() - 1).addMaterial(materials.get(i+2)); // works fine
}
}
Eclipse error: The method add(int, makeMat.Maker) in the type ArrayList<makeMat.Maker> is not applicable for the arguments (int)
.
I thought that something like what is mentioned in this post would be the fix, but I still get the same error when I modify the add
line and include two additional lines before it:
Maker mk = new Maker(makId);
mk.setMakId(makId);
makers.add(mk);
If I understand correctly, the issue is that when I use .add
here I need to be adding an object, not an int
. So, the solution should be to just add int
to an object, and then add that to my ArrayList<Object>
. I thought that that's what I'm doing in the modification, but it still throws the same error.