1

I currently have 2 classes. My bot:

public class Bot {
    private int X;
    private int Y;
    private int degress;

    public int getX() {
        return X;
    }

    public int getY() {
        return Y;
    }

    public int getDegress() {
        return degress;
    }

    public void setX(int X) {
        this.X = X;
    }

    public void setY(int Y) {
        this.Y = Y;
    }
}

and my main code.. the problem i am having is when trying to set the degrees in my main class. What I am trying to do is something similar to:

bots.set(bots.get(1).getDegress(),+1);

but gives me this error "The method set(int, Bot) in the type ArrayList is not applicable for the arguments (int, int)"

and how my ArrayList looks like

public static ArrayList<Bot> bots = new ArrayList<Bot>();

So to sum it up. How could i come about to change X, Y or Degress with the bots.set?

1
  • Do you want to increase the degrees with 1? Because then change bots.set(bots.get(1).getDegress(),+1); to bots.set(bots.get(1).getDegress()+1); Commented Dec 17, 2018 at 13:56

2 Answers 2

3

bots.set is an ArrayList method. To mutate a Bot instance, you must call a Bot method.

It should be:

Bot bot = bots.get(1);
bot.setDegrees(bot.getDegrees()+1);

Or course, you'll need a setDegrees method in your Bot class.

Sign up to request clarification or add additional context in comments.

3 Comments

Alright thanks! and yeah i got one already forgot to include it.
Didn't seem to work. Gave me this error "The method getDegrees() is undefined for the type ArrayList<Bot>"
@KappaCoder please notice it's bot.getDegrees(), not bots.getDegrees()
1

bots.set(bots.get(1).getDegress(),+1); at this line you are calling the Set method of ArrayList class, which is used to replace an element at the specified index in the list. It takes 2 parameters, first the index of the element to be replaced and 2nd the element to be replaced with. So in your case an int and an instance of Bot class.

To set degrees in you Bot class object you'll first have to get it from the list bots and use setDegrees() on the object of bot class(which btw seems to be missing from your Bot class).

bots.get(1).setDegrees(bots.get(1).getDegress()+1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.