[英]ArrayList cannot resolve variable type
Attempting to get a custom class known as DrawGraphics to contain an ArrayList of custom objects instead of a single Sprite. 试图获取一个称为DrawGraphics的自定义类,以包含一个自定义对象的ArrayList而不是单个Sprite。 However the ArrayList refuses to accept the new Bouncer object, and when it does, the rest of the DrawGraphics class does not recognize it.
但是,ArrayList拒绝接受新的Bouncer对象,并且当它接受时,DrawGraphics类的其余部分无法识别它。
Original Code 原始码
package objectssequel;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Graphics;
public class DrawGraphics {
Bouncer movingSprite; //this was the original single sprite
/** Initializes this class for drawing. */
public DrawGraphics() {
Rectangle box = new Rectangle(15, 20, Color.RED);
movingSprite = new Bouncer(100, 170, box);
movingSprite.setMovementVector(3, 1);
}
/** Draw the contents of the window on surface. */
public void draw(Graphics surface) {
movingSprite.draw(surface);
}
}
Attempted Solution: First I created the ArrayList of objects of class Bouncer 尝试的解决方案:首先,我创建了Bouncer类的对象的ArrayList
ArrayList<Bouncer> bouncerList = new ArrayList<Bouncer>();
All good. 都好。 I first then inserted the following code on the line below this
我首先在下面的行中插入以下代码
bouncerList.add(movingSprite);
This generated a "Syntax error on token(s), misplaced construct(s)" and "Syntax error on token "movingSprite", VariableDeclaratorId expected after this token" compiler error. 这会生成“令牌上的语法错误,构造放置不正确”和令牌上的“ movingSprite”令牌语法错误,在此令牌之后需要VariableDeclaratorId”编译器错误。 I guessed this might be because I used the bouncerList.add() outside of a method body, so I created the following method for the class DrawGraphics
我猜这可能是因为我在方法主体之外使用了bouncerList.add(),所以我为DrawGraphics类创建了以下方法
public void addBouncer(Bouncer newBouncer) {
bouncerList.add(newBouncer);
}
I then called this method in DrawGraphics() with: 然后,我在DrawGraphics()中使用以下方法调用此方法:
addBouncer(movingSprite);
the compiler error informed me that movingSprite could not be resolved to a variable type. 编译器错误通知我,movingSprite无法解析为变量类型。 I attempted this:
我试图这样做:
public void addBouncer() {
Bouncer movingSprite;
bouncerList.add(movingSprite);
}
Then attempted to initialize movingSprite by giving it a null setting, and no such luck as well, and probably a dozen other combinations ways to fix this. 然后尝试通过给其设置为null设置来初始化movingSprite,也没有这种运气,可能还有十二种其他组合方式来解决此问题。 Any solutions?
有什么办法吗? How do I create an ArrayList of Bouncer objects within the DrawGraphics Class?
如何在DrawGraphics类中创建Bouncer对象的ArrayList?
Edit: Is it possible to not use and remove 'Bouncer movingSprite' from the original code and create an instance of the object just from the bouncerList.add()? 编辑:是否有可能不使用和从原始代码中删除'Bouncer movingSprite'并仅从bouncerList.add()创建对象的实例?
In this code 在这段代码中
public void addBouncer(Bouncer newBouncer) {
bouncerList.add(Bouncer); // this is trying to add a class
}
you need to change to 您需要更改为
public void addBouncer(Bouncer newBouncer) {
bouncerList.add(newBouncer); // this will add the object
}
and after 之后
movingSprite.setMovementVector(3, 1);
call 呼叫
addBouncer (movingSprite);
You are trying to declare and initialize the array at object construction time? 您是否正在尝试在对象构造时声明和初始化数组? Sadly, java collections makes this seemingly obvious use case awkward.
可悲的是,java集合使这种看似显而易见的用例变得笨拙。
List< Bouncer > bouncerList = new ArrayList< Bouncer >() { {
add( new Bouncer() );
} };
This may lead to difficulty serializing your DrawGraphics class if ever that becomes necessary. 如果有必要,这可能导致难以序列化DrawGraphics类。 Why not populate it in the DrawGraphics constructor?
为什么不将其填充到DrawGraphics构造函数中?
Another option: 另外一个选项:
List< Bouncer > bouncerList = new ArrayList< Bouncer >( Arrays.asList( new Bouncer() ) );
You can also use guava's Lists utility class to construct and populate an array list in a single line. 您还可以使用guava的Lists实用程序类在一行中构造和填充数组列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.