从大量文本字段中获取和打印数据的最佳方法?

发布于 2025-01-07 14:27:25 字数 295 浏览 2 评论 0原文

我正在尝试找出从大约 60 个文本字段读取数据然后将其打印在空格行中的最佳方法。

例如

System.out.println(field1.getText() + " " + field2.getText());

问题是,我不想有大量的 getText() 方法。

所以我的主要问题是,是否有一种更简单、也许更好(性能方面)的方法来做到这一点?

图像: 我的文本字段

I'm trying to figure out the best way to read data from around 60 text fields and then print it in a spaced line.

For example

System.out.println(field1.getText() + " " + field2.getText());

The thing is, I don't want to have a crap load of getText() method.

So my main question is, is there an easier and perhaps better (performance-wise) way to do this?

Image:
my text fields

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

清旖 2025-01-14 14:27:25

您可以使用 StringBuilder 而不是字符串连接以获得更好的性能:

StringBuilder s = new StringBuilder();
s.append(field1.getText()).append(" ")
 .append(field2.getText()).append(" ")
 .append(field3.getText());
System.out.println(s.toString());

如果您的字段存储在集合中,您可以迭代该集合而不是调用每个单独的字段:

List<JTextField> fields = new ArrayList<JTextField> ();
for(int i = 0; i < 30; i++) {
    fields.add(new JTextField(Integer.toString(i)));
}
//...
StringBuilder s = new StringBuilder();
for (JTextField t : fields) {
    s.append(t.getText()).append(" ");
}
//...

You could use StringBuilder instead of string concatenation for better performance:

StringBuilder s = new StringBuilder();
s.append(field1.getText()).append(" ")
 .append(field2.getText()).append(" ")
 .append(field3.getText());
System.out.println(s.toString());

And if your fields were stored in a collection you could iterate over that collection instead of calling each individual field:

List<JTextField> fields = new ArrayList<JTextField> ();
for(int i = 0; i < 30; i++) {
    fields.add(new JTextField(Integer.toString(i)));
}
//...
StringBuilder s = new StringBuilder();
for (JTextField t : fields) {
    s.append(t.getText()).append(" ");
}
//...
半山落雨半山空 2025-01-14 14:27:25

我想不出任何其他性能更好的方法。

首先,

  • 使用 TextField 数组或列表来保存所有文本字段。然后你
    可以使用 for 循环迭代它们。

其次,

  • 尽可能使用 StringBuilder。

I couldn't think of any other performance wise better method.

Firstly,

  • Use TextField array or list to hold all your text fields. Then you
    could iterate them using a for loop.

Secondly,

  • Use StringBuilder whereever possible.
剧终人散尽 2025-01-14 14:27:25
  1. 将字段添加到数组或集合中,然后在 for-each 循环中迭代它们,打印输出。

  2. 在类上使用反射,通过某种命名约定来标识字段,例如正则表达式 textField.+

  3. 编写自己的注释,使其在运行时可保留,并对要访问的每个字段进行注释,然后使用反射来迭代类字段,检查注释,并打印文本字段的值(如果有) 编写您自己的注释

如果有良好的命名约定/模式,2. 是最好的。 3.有点重,但是非常精确和灵活。 1.也是一种快速而简单的破解方法。

  1. Add your fields to an array or collection, then iterate over them in a for-each loop, printing out the output.

  2. Use reflection over the class, to identify the fields by some naming convention, like the regular expression textField.+

  3. Write your own annotation, make it retainable in runtime, and annotate every field you want to visit, then use reflection to iterate over class fields, checking for the annotation, and printing the value of the text field if it has the annotation attached.

With good naming convention/pattern, 2. is best. 3. is a bit heavyweight, but very precise and flexible. 1. is a quick and easy hack too.

携君以终年 2025-01-14 14:27:25

您可以尝试使用反射。但从性能角度来看,它会更慢:

public class Snippet {

    JTextField field1 = new JTextField("1");
    JTextField field2 = new JTextField("2");

    JTextField field3 = new JTextField("3");
    JTextField field4 = new JTextField("4");

    // add more fields here

    public static void main(String[] args) throws Exception {
        new Snippet().run();
    }

    private void run() throws Exception {
        for (int i = 1; i <= 4; i += 2) {
            JTextField textfieldA = getTextField(i);
            JTextField textfieldB = getTextField(i + 1);
            System.out.println(textfieldA.getText() + " " + textfieldB.getText());
        }
    }

    private JTextField getTextField(int i) throws NoSuchFieldException, IllegalAccessException {
        Field field = Snippet.class.getDeclaredField("field" + i);
        JTextField textfield = (JTextField) field.get(this);
        return textfield;
    }
}

这既快又脏,从长远来看,您最好将文本字段放入适当的数据结构(例如列表或地图)中,然后简单地对其进行迭代。

You could try using reflection. Performance-wise it will be slower though:

public class Snippet {

    JTextField field1 = new JTextField("1");
    JTextField field2 = new JTextField("2");

    JTextField field3 = new JTextField("3");
    JTextField field4 = new JTextField("4");

    // add more fields here

    public static void main(String[] args) throws Exception {
        new Snippet().run();
    }

    private void run() throws Exception {
        for (int i = 1; i <= 4; i += 2) {
            JTextField textfieldA = getTextField(i);
            JTextField textfieldB = getTextField(i + 1);
            System.out.println(textfieldA.getText() + " " + textfieldB.getText());
        }
    }

    private JTextField getTextField(int i) throws NoSuchFieldException, IllegalAccessException {
        Field field = Snippet.class.getDeclaredField("field" + i);
        JTextField textfield = (JTextField) field.get(this);
        return textfield;
    }
}

This is quick and dirty and in the long run you will be better off putting your textfields into an appropriate data structure (e.g. List or Map) and simply iterate over it.

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