循环遍历 Java 数组并追加 () 到 TextArea
几天后,我仍然无法像在其他语言中那样通过数组来填充文本区域。我已经尝试过 Google、YouTube、stackoverflow 等,但仍然无法使用任何示例来帮助我做到这一点。我还参考了 Java 文本。这正是我想要做的:
public void getDrinks() {
//System.out.println(theDrinks[arrayCount].toString());
for(int i=0; i<arrayCount; i++) {
area.append(theDrinks[i].toString());
}
}
此代码适用于其他语言,但我使用 TextArea
或数组的方式有问题,因为我得到了一个空指针。我很想粘贴整个程序,但这也不起作用。这是唯一可以正确远程粘贴的部分。如果可以的话请帮助我。
After more than a couple of days I am still unable to populate a text area by going through an array as I can in other languages. I have tried Google, YouTube, stackoverflow, and others and I am still unable to use any examples to help me do this. I have also referenced Java texts. Here is exactly what I am trying to do:
public void getDrinks() {
//System.out.println(theDrinks[arrayCount].toString());
for(int i=0; i<arrayCount; i++) {
area.append(theDrinks[i].toString());
}
}
This code works in other languages but something is wrong with the way I am using the TextArea
or the array because I am getting a null pointer. I would love to paste the entire program, but that is not working either. This is the only part that will even remotely paste correctly. Please help me if you can.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,当你有能力时,你不想使用像 arrayCount 这样的变量。更好的版本是这样的:
执行此操作时,重要的是要确保
area
已经实例化(即它不是null
)。如果我要实现这个,我会使用 Java 的 foreach 构造,因为我发现它更具表现力。以下代码假定
theDrinks
是Drink
对象的数组。You do not, in general, want to use a variable like
arrayCount
when you can help it. A better version is this:When doing this, it is important to make sure that
area
has been instantiated already (i.e. it is notnull
).If I were implementing this, I would use Java's foreach construct instead, as I find it's a bit more expressive. The following code assumes that
theDrinks
is an array ofDrink
objects.如果没有更多代码,我无法弄清楚你的具体问题是什么,但这里有一些有效的 java 代码。
I can't way whay your specific problem can be without more code but here some java code that works.