什么是temp以及temp在java中的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27331274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is temp and what is the use of temp in java?
提问by selva
I am getting started to learn Java and I wrote a simple array example program,
我开始学习Java,我写了一个简单的数组示例程序,
public class ExampleArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {10, 20, 30, 40, 50};
System.out.println(a[2] + " " + a[4]);
for (int temp : a) {
System.out.println(temp);
}
}
}
This is the output:
这是输出:
30 50
10
20
30
40
50
It prints all the values from an array.
它打印数组中的所有值。
May I know, what is the use of temp
in Java?
我可以知道,temp
Java 中的用途是什么?
Can anyone explain this keyword please?
谁能解释一下这个关键字?
采纳答案by Tom
temp
is not a keyword, it is just a name for a local variable. You can call it temp
, blub
or better: entry
or value
(to have a meaningful name for this variable).
temp
不是关键字,它只是局部变量的名称。您可以将其称为temp
,blub
或者更好:entry
或value
(为此变量指定一个有意义的名称)。
for(int temp: a)
means literally: take each element from array (or any other Iterable
) a
separately and write it in the variable temp
of type int
, so the loop body can use that variable / array element.
The code in your example then use this variable to print it to the console.
for(int temp: a)
字面意思是:分别从数组(或任何其他Iterable
)中取出每个元素a
并将其写入temp
type的变量中int
,因此循环体可以使用该变量/数组元素。您示例中的代码然后使用此变量将其打印到控制台。
回答by Remees M Syde
Here temp
is nothing but a variable, which used to iterate the value of the array a
.Which get the values one by one from array.This is actually happening there
这里temp
只是一个变量,用于迭代数组a
的值。从数组中一个一个地获取值。这实际上发生在那里
for(Iterator<String> temp = someList.iterator(); temp.hasNext(); ) {
String item = temp.next();
System.out.println(item);
}
FYI: There is no need of using a name temp
, Its a variable you can use as you wish and there is nothing called temp
there in java.You can use any java variable name.
仅供参考:不需要使用 name temp
,它是一个您可以随意使用的变量,并且temp
在 java 中没有任何名称。您可以使用任何 java 变量名称。
回答by Max Klein
temp
in this case is the name of a local variable (an integer).
temp
在这种情况下是局部变量的名称(整数)。
for(int temp : a){
System.out.println(temp);
}
This code iterates through the array named a
. In each iteration temp
gets assigned the next value of a
. System.out.println(temp);
just prints the value of temp
to the console.
此代码遍历名为 的数组a
。在每次迭代中temp
都分配了下一个值a
。System.out.println(temp);
只是将 的值打印temp
到控制台。