[英]How to correctly use the add method in Java?
I need to add the Rectangle to the ArrayList called bricks as you can see below. 我需要将Rectangle添加到名为Bricks的ArrayList中,如下所示。
private void drawBrick(int startX, int startY){
new Rectangle();
currentColor = 0;
startX = 54;
startY = 16;
bricks = new ArrayList<Rectangle>();
bricks.add("Rectangle");
}
I keep getting compilation errors after adding that that last line of code and this weird warning pops up saying "Some messages have been simplified; recompile with -Xdiags:verbose to get full output." 添加最后一行代码后,我不断收到编译错误,并弹出奇怪的警告,提示“某些消息已被简化;使用-Xdiags:verbose重新编译以获取完整的输出。”
Anyone know what I've done wrong? 有人知道我做错了吗?
You can't just refer to the Rectangle
you created by using "Rectangle"
. 你不能只是指
Rectangle
创建通过使用"Rectangle"
。 Instead, you need to name the Rectangle
when you create it and use that name later on, like this: 相反,您需要在创建
Rectangle
时为其命名,并在以后使用该名称,如下所示:
private void drawBrick(int startX, int startY) {
Rectangle theRectangle = new Rectangle();
currentColor = 0;
startX = 54;
startY = 16;
bricks = new ArrayList<Rectangle>();
bricks.add(theRectangle);
}
Notice how that creates a variable called theRectangle
which is set equal to the Rectangle
you create, then that specific Rectangle
( theRectangle
) is added to the ArrayList
called bricks
. 注意如何创建一个名为
theRectangle
的变量,该变量设置为等于您创建的Rectangle
,然后将特定的Rectangle
( theRectangle
)添加到名为bricks
的ArrayList
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.