首页 > 解决方案 > ArrayList: How to arraylist.set when you have multiple setters

问题描述

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?

标签: javaarraylistset

解决方案


bots.set是一种ArrayList方法。要改变一个Bot实例,你必须调用一个Bot方法。

它应该是:

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

或者,当然,你的课堂上需要一个setDegrees方法。Bot


推荐阅读