[英]How can i change an arraylist object (int , string, boolean “false”) to (int,String,boolean“true”) and return it back to the array list?
[英]How can I change an int to a String for this object?
我在这里学习Java,并使用“ GLabel”对象。 它在ACM图形库中,您可以在这里阅读有关它的信息:
http://jtf.acm.org/javadoc/student/acm/graphics/GLabel.html
简而言之,GLabel打印一个字符串。 事情是我有一个要在此处打印的整数。 我将如何实现?
这是我拥有的循环,但是由于存在int,因此无法编译。
for( int k = 1; i> 5; k++){
GLabel counter = new GLabel(k);
add(counter, (getWidth() / 2), (getHeight() / 2) );
pause (500);
remove(counter);
}
当我尝试这样做时: GLabel( (String)k);
我收到“无法从int转换为String”错误...
编辑:我已经看到了setLabel方法-也许这是唯一的方法吗?
试试GLabel("" + k);
代替。
您不能将int
转换为String
,因此必须进行转换。 有多种方法可以执行此操作,但一种简单的方法是将其附加到空字符串中。
其他方法:
使用+
字符串串联“技巧”的一个警告是,您需要考虑运算符的优先级。
System.out.println("" + 1 + 2); // this prints "12"
System.out.println("" + (1 + 2)); // this prints "3"
Integer.toString(k)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.