Java - 访问for循环内外的变量 [英] Java - Access variable inside and outside of for-loop
问题描述
String playerlist;
(Player p:allplayers){
playerlist + = p.getDisplayName()+,;
}
sendMessage(playerlist);
我该如何做到这一点?我需要在我的for循环中有一个变量,然后在循环之外访问它。
谢谢,如果这是一个非常不好的问题,非常抱歉。我只是想不出来。
你很近。您需要将第一行更改为
String playerlist =;
在Java中,在初始化之前使用变量是非法的,而行
playerlist + = p.getDisplayName()+,;
desugars(*)to
String temp = playerlist;
playerlist = temp +(p.getDisplayName()+,);
初始化 playerlist
将其内容读入临时变量,然后更新 playerlist
。
有两个更高级别的建议如果你有一个可用的连接方法,就像一些库提供的那样(就像 )。 /msdn.microsoft.com/en-us/library/system.string.join.aspxrel =nofollow>。NET 和 编辑:正如@edalorzo在评论中正确地提到,你只会得到一个错误,不会初始化一个本地变量,并且静态和实例字段都是自动的初始化。具体而言,数字类型( (*):desugaringI上面显示的几乎是准确的:实际上,
int
, long
, double
)等等)被初始化为0,布尔值被初始化为 false
,并且引用类型被初始化为 null
该规范明确描述了字段的自动初始化,所以你可以依靠它,但它很少是你想要的。通常情况下,稍后您将需要使用该字段的非默认值,因为 null
不是非常有用(许多语言,特别是标准ML ,完全没有 null
)。所以我建议在构造函数中初始化所有对象的字段。
$ b playerlist
只计算一次,不影响这个特定程序的行为。
String playerlist;
for(Player p : allplayers){
playerlist += p.getDisplayName() + ", ";
}
sendMessage(playerlist);
How can I accomplish this? I need to have a variable inside my for-loop, and then access it outside of the loop.
Thanks, and sorry if this is a very nooby question. I just can't figure it out.
You're close. You need to change the first line to
String playerlist = "";
In Java it's illegal to use a variable before it's initialized, and the line
playerlist += p.getDisplayName() + ", ";
desugars(*) to
String temp = playerlist;
playerlist = temp + (p.getDisplayName() + ", ");
Once you initialize playerlist
, you are able to read off its contents into the temporary variable, and then update playerlist
.
There are two higher-level suggestions, though.
- If you have a join method available, as some libraries provide (as do the the .NET and Python platforms), you can just use a join, which is easier to read.
- However, if you don't have a join method available (AFAIK, the Java standard library provides no such thing), you should use a StringBuilder, consistent with @anubhava's suggestion. You'll get better performance on large numbers of strings, and the code is easier to understand, which is a win-win.
EDIT: As @edalorzo rightly mentions in the comments, you will only get an error for not initializing a local variable, and both static and instance fields are automatically initialized. Specifically, numeric types (int
, long
, double
, etc.) are initialized to 0, booleans are initialized to false
, and reference types are initialized to null
.
The spec explicitly describes the automatic initialization of fields, so you can rely on it, but it's rarely what you want. Normally, you will want a non-default value of the field when you use it later, since null
isn't terribly useful (and many languages, particularly functional languages like Standard ML, go without null
altogether). So I would recommend initializing all the fields of your objects in the constructor.
(*): The "desugaring" I show above is almost accurate: in truth, playerlist
is evaluated only once, which doesn't affect the behavior of this particular program.
这篇关于Java - 访问for循环内外的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!