什么是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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:11:13  来源:igfitidea点击:

What is temp and what is the use of temp in java?

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 tempin Java?

我可以知道,tempJava 中的用途是什么?

Can anyone explain this keyword please?

谁能解释一下这个关键字?

采纳答案by Tom

tempis not a keyword, it is just a name for a local variable. You can call it temp, blubor better: entryor value(to have a meaningful name for this variable).

temp不是关键字,它只是局部变量的名称。您可以将其称为tempblub或者更好:entryvalue(为此变量指定一个有意义的名称)。

for(int temp: a)means literally: take each element from array (or any other Iterable) aseparately and write it in the variable tempof 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并将其写入temptype的变量中int,因此循环体可以使用该变量/数组元素。您示例中的代码然后使用此变量将其打印到控制台。

回答by Remees M Syde

Here tempis 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 tempthere in java.You can use any java variable name.

仅供参考:不需要使用 name temp,它是一个您可以随意使用的变量,并且temp在 java 中没有任何名称。您可以使用任何 java 变量名称。

回答by Max Klein

tempin 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 tempgets assigned the next value of a. System.out.println(temp);just prints the value of tempto the console.

此代码遍历名为 的数组a。在每次迭代中temp都分配了下一个值aSystem.out.println(temp);只是将 的值打印temp到控制台。