使用 for 循环和 .setText() 方法打印数组?

发布于 2024-12-18 00:44:10 字数 319 浏览 0 评论 0原文

我正在使用它来尝试显示数组中的所有字符,但它打印的只是“g” 我在常规 java 中使用 System.out.println() 测试了相同的代码,它工作正常,

        String testarray[]={"a","s","d","f","g"};

    for(int l=0; l<testarray.length; l++){
        String temp="";
        temp=temp+testarray[l];
        display.setText(""+temp);
    }

谢谢!

I am using this to try to display all the characters in the array but all it prints is "g"
I tested the same code using System.out.println() in regular java and it works fine

        String testarray[]={"a","s","d","f","g"};

    for(int l=0; l<testarray.length; l++){
        String temp="";
        temp=temp+testarray[l];
        display.setText(""+temp);
    }

Thanks!

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

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

发布评论

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

评论(3

绳情 2024-12-25 00:44:10

通过您的代码,我将避免其他人所说的内容,我将用简单的外行术语向您解释:

用简单的英语,setprint之间存在差异em>.

当您设置某些内容时,您会将其中的任何内容替换为新值。当您打印时,您附加一些内容到那里的任何内容上。 System.out.print 因此会附加内容,而 setText 则会设置内容。

for 发生得如此之快(计算机可以比你眨眼的速度更快地计算出无数的东西),以至于你会连续看到一堆 setText 东西,最后只得到“g” 。正如其他人所说,这就是为什么您需要“附加”内容。

然而,比其他人所说的更简单,在整个 for 循环中只需要一行。

display.setText(display.getText() + testarray[l]);

这就是您所需要的。

By your code, I will avoid what others are saying and I'll explain to you in simple laymen terms:

In plain english, there is a difference between set and print.

When you set something, you replace whatever was there with a new value. When you print, you append something to whatever was there. System.out.print thus appends stuff, while setText sets stuff instead.

The for happens so fast (computer can compute gazillion stuffs faster than you can blink) that you see a bunch of setText stuff in succession, and end up with only "g". That's why you need, as others said, to "append" stuff.

However, simpler than what others have said, there is only one line that you need inside this whole for loop.

display.setText(display.getText() + testarray[l]);

And that is all you need.

新人笑 2024-12-25 00:44:10

执行此操作的最佳方法是使用 for-each 循环。

尝试:

TextView tv = (TextView) findViewById(R.id.TextView);
String testarray[] = {"a","s","d","f","g"};

String print = "";
  for(String s : testarray) {
    print += s;
  }

tv.setText(print)

这将打印出 asdfg

代码中的问题是您在 for 循环内初始化变量,这意味着每次循环时,它都会重新创建 String = "",以修复此初始化,就像我在示例中所做的那样。另外,在循环结束之前不要设置文本视图。

如果您不熟悉 for-each 循环,它基本上是:

for each ( object x in objectArray)
  do this

编辑:

System.out.println() != .setText()

.setText() 设置视图的文本,每次重置文本时,您确实这样做,删除旧文本,然后添加新文本

The best way to do this, is with a for-each loop.

Try:

TextView tv = (TextView) findViewById(R.id.TextView);
String testarray[] = {"a","s","d","f","g"};

String print = "";
  for(String s : testarray) {
    print += s;
  }

tv.setText(print)

This will print out asdfg

The problem in your code is you initialize the variable WITHIN the for loop, meaning for each time it goes through, it remakes the String = "", to fix this initialize it like I do in my example. Also, don't set the textview until after the loop.

If you're not familiar with the for-each loop, its basically:

for each ( object x in objectArray)
  do this

EDIT:

System.out.println() != .setText()

.setText() SETS the text of the view, every time you reset the text, you do exactly that, remove the old text, and add the new

最笨的告白 2024-12-25 00:44:10

你做错了。你在 for 循环中用空字符串初始化临时字符串。
因此,每次它将分配空字符串,然后您从数组连接字符串,因此临时字符串将只有您从数组分配的字符串,然后在文本视图上显示。

这样您可以通过在 Textview 上创建字符串来设置所有字符

String testarray[]={"a","s","d","f","g"};
String temp="";

for(int l=0; l<testarray.length; l++){
   temp=temp+testarray[l];
}

display.setText(temp);

You are doing wrong way.You are initializing the temp string with empty string in for loop.
So every time it will be assign empty string and then you are concating the string from array so temp string will have only String which you are assigning from array and then you are displaying on textview.

This way you can set all the character by creating string on Textview

String testarray[]={"a","s","d","f","g"};
String temp="";

for(int l=0; l<testarray.length; l++){
   temp=temp+testarray[l];
}

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