使用 for 循环和 .setText() 方法打印数组?
我正在使用它来尝试显示数组中的所有字符,但它打印的只是“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过您的代码,我将避免其他人所说的内容,我将用简单的外行术语向您解释:
用简单的英语,set和print之间存在差异em>.
当您设置某些内容时,您会将其中的任何内容替换为新值。当您打印时,您附加一些内容到那里的任何内容上。
System.out.print
因此会附加内容,而setText
则会设置内容。for
发生得如此之快(计算机可以比你眨眼的速度更快地计算出无数的东西),以至于你会连续看到一堆setText
东西,最后只得到“g” 。正如其他人所说,这就是为什么您需要“附加”内容。然而,比其他人所说的更简单,在整个 for 循环中只需要一行。
这就是您所需要的。
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, whilesetText
sets stuff instead.The
for
happens so fast (computer can compute gazillion stuffs faster than you can blink) that you see a bunch ofsetText
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.
And that is all you need.
执行此操作的最佳方法是使用 for-each 循环。
尝试:
这将打印出 asdfg
代码中的问题是您在 for 循环内初始化变量,这意味着每次循环时,它都会重新创建 String = "",以修复此初始化,就像我在示例中所做的那样。另外,在循环结束之前不要设置文本视图。
如果您不熟悉 for-each 循环,它基本上是:
编辑:
System.out.println() != .setText()
.setText() 设置视图的文本,每次重置文本时,您确实这样做,删除旧文本,然后添加新文本
The best way to do this, is with a for-each loop.
Try:
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:
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
你做错了。你在 for 循环中用空字符串初始化临时字符串。
因此,每次它将分配空字符串,然后您从数组连接字符串,因此临时字符串将只有您从数组分配的字符串,然后在文本视图上显示。
这样您可以通过在 Textview 上创建字符串来设置所有字符
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