循环遍历 Java 数组并追加 () 到 TextArea

发布于 2025-01-07 02:03:45 字数 441 浏览 2 评论 0原文

几天后,我仍然无法像在其他语言中那样通过数组来填充文本区域。我已经尝试过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2025-01-14 02:03:46

一般来说,当你有能力时,你不想使用像 arrayCount 这样的变量。更好的版本是这样的:

public void getDrinks() {
    for(int i = 0; i < theDrinks.length; i++)
        area.append(theDrinks[i].toString());
}

执行此操作时,重要的是要确保 area 已经实例化(即它不是 null)。

如果我要实现这个,我会使用 Java 的 foreach 构造,因为我发现它更具表现力。以下代码假定 theDrinksDrink 对象的数组。

public void getDrinks() {
    if(area != null) {
        for(Drink drink : theDrinks) {
            area.append(drink.toString());
        }
    }
}

You do not, in general, want to use a variable like arrayCount when you can help it. A better version is this:

public void getDrinks() {
    for(int i = 0; i < theDrinks.length; i++)
        area.append(theDrinks[i].toString());
}

When doing this, it is important to make sure that area has been instantiated already (i.e. it is not null).

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 of Drink objects.

public void getDrinks() {
    if(area != null) {
        for(Drink drink : theDrinks) {
            area.append(drink.toString());
        }
    }
}
人疚 2025-01-14 02:03:46

如果没有更多代码,我无法弄清楚你的具体问题是什么,但这里有一些有效的 java 代码。

import java.awt.BorderLayout;
import java.awt.TextArea;

import javax.swing.JFrame;

public class Driver {

    public static void main(String[] args) {
        // 1. Create the frame.
        JFrame frame = new JFrame("FrameDemo");

        // 2. Optional: What happens when the frame closes?
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 3. Create components and put them in the frame.
        // ...create emptyLabel...

        TextArea area = new TextArea("Area");
        frame.getContentPane().add(area, BorderLayout.CENTER);

        // 4. Size the frame.
        frame.pack();

        // 5. Show it.
        frame.setVisible(true);

        String[] drinks = {"Drink1","Drink2"};
        getDrinks(area, drinks);
    }

    public static void getDrinks(TextArea area, String[] theDrinks) {
        // System.out.println(theDrinks[arrayCount].toString());

        for (String drink : theDrinks) {

            area.append(drink.toString());

        }

    }

}

I can't way whay your specific problem can be without more code but here some java code that works.

import java.awt.BorderLayout;
import java.awt.TextArea;

import javax.swing.JFrame;

public class Driver {

    public static void main(String[] args) {
        // 1. Create the frame.
        JFrame frame = new JFrame("FrameDemo");

        // 2. Optional: What happens when the frame closes?
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 3. Create components and put them in the frame.
        // ...create emptyLabel...

        TextArea area = new TextArea("Area");
        frame.getContentPane().add(area, BorderLayout.CENTER);

        // 4. Size the frame.
        frame.pack();

        // 5. Show it.
        frame.setVisible(true);

        String[] drinks = {"Drink1","Drink2"};
        getDrinks(area, drinks);
    }

    public static void getDrinks(TextArea area, String[] theDrinks) {
        // System.out.println(theDrinks[arrayCount].toString());

        for (String drink : theDrinks) {

            area.append(drink.toString());

        }

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文