-2

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.

user8121557
  • 149
  • 2
  • 9
  • “I still get the same error”—You shouldn’t. Perhaps after you made the `makers.add(mk);` change, you forgot to save the file before compiling. – VGR Oct 05 '20 at 02:10

1 Answers1

0

What you want to add to the makers ArrayList is what you declared the makers parameter to be (if it in fact actually exists) and that would be a new instance of Maker.

It's easiest if the Maker class contains a constructor which can fill the required class instance member variables, for example if the Maker class consisted of:

public class Maker {

    // class Member variables
    private int makeID;
    private String makeName;
    private String makeModel;

    // Constructor 1:
    public Maker() { }

    // Contructor 2:
    public Maker(int id, String name, String model) {
        this.makeID = id;
        this.makeName = name;
        this.makeModel = model;
    }

    // Getters & Setters
    public int getMakeID() {
        return makeID;
    }

    public void setMakeID(int id) {
        this.makeID = id;
    }

    public String getMakeName() {
        return makeName;
    }

    public void setMakeName(String name) {
        this.makeName = name;
    }

    public String getMakeModel() {
        return makeModel;
    }

    public void setMakeModel(String model) {
        this.makeModel = model;
    }

    @Override
    public String toString() {
        return new StringBuilder("").append(String.valueOf(this.makeID)).append(", ")
                   .append(this.makeName).append(", ").append(this.makeModel).toString();
    }
} 

In your posted method code example you would have something like this:

makers.add(new Maker(makId, makName, makModel));

This now adds a new instance of a Maker object into the makers ArrayList collection.

DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22