java 如何在单个语句中定义多个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30248287/
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-11-02 16:45:33  来源:igfitidea点击:

How to define multiple variables in single statement

javaarrays

提问by Nasreddin

In Python, I can define two variables with an array in one line.

在 Python 中,我可以在一行中用一个数组定义两个变量。

>>>[a,b] = [1,2]
>>>a
1
>>>b
2

How do I do the same thing in Java?

我如何在 Java 中做同样的事情?

I have a couple of variables in class PCT which type is final. Is there a way to define them in one line in a Python like fashion? The following format clearly does not work in Java. I could define them separately, but it will call the parseFile method twice which I want to avoid.

我在 PCT 类中有几个变量,它们的类型是最终的。有没有办法以类似 Python 的方式在一行中定义它们?以下格式显然不适用于 Java。我可以分别定义它们,但它会调用 parseFile 方法两次,这是我想避免的。

public class PCT {
    final int start;
    final int stop;
    public PCT (File file) {
        //......
        //......
        // the following statement does not compile
        [start, stop] = parseFile(file);
    }
    public int[] parseFile(File f) {
        int[] aa = new int[2];
        // ....
        // ....
        return aa;
    }
}

采纳答案by Jeremy Barnes

This is not possible, but you also don't need to call parseFiletwice.

这是不可能的,但您也不需要调用parseFile两次。

Write your code like this:

像这样写你的代码:

int [] temp = parseFile(file);
start = temp[0];
stop = temp[1];

Python (I believe) supports multiple return values. Java obeys C conventions, and so doesn't permit it. Since that isn't part of the language, the syntax for it isn't either, meaning slightly gross hacks like the temparray are needed if you're doing multiple returns.

Python(我相信)支持多个返回值。Java 遵守 C 约定,因此不允许这样做。由于这不是语言的一部分,它的语法也不是,这意味着temp如果您要进行多次返回,则需要像数组这样的稍微粗暴的技巧。

回答by Syed Shaharyaar Hussain

You can define multiple variables like this :

您可以像这样定义多个变量:

double a,b,c;

Each variable in one line can also be assigned to specific value too:

一行中的每个变量也可以分配给特定的值:

double a=3, b=5.2, c=3.5/3.5;

One more aspect is, while you are preparing common type variable in same line then from right assigned variables you can assign variable on left, for instance :

另一个方面是,当您在同一行中准备公共类型变量时,您可以从右侧分配的变量中分配左侧的变量,例如:

int a = 4, b = a+1, c=b*b;

Noticed, you can also practice arithmetic operations on variable by remaining in the same line.

请注意,您还可以通过保留在同一行中来练习对变量进行算术运算。

回答by Willem Van Onsem

If you literaly mean line; as long as you place a semicolon in between two statements, they are executed as if there is a new line in between so you can call:

如果你字面意思是line; 只要在两个语句之间放置一个分号,它们就会被执行,就好像中间有一个新行一样,因此您可以调用:

a = 1; b = 2;

You can even compress an entire file into a oneliner, by removing comment (that scope to the end of the line). Spacing (space, tab, new line,...) is in general removed from the Java files (in memory) as first step in the Java compiler.

您甚至可以通过删除注释(该范围到行尾)将整个文件压缩为一个单行。作为 Java 编译器的第一步,间距(空格、制表符、换行符...)通常从 Java 文件(在内存中)中删除。

But you are probably more interested in a singe statement. Sytax like [start, stop] = parseFile(file);is not supported (at least not for now). You can make a onliner:

但您可能对单个语句更感兴趣。[start, stop] = parseFile(file);不支持类似语法(至少现在不支持)。你可以做一个在线人:

int[] data = parseFile(file); start = data[0]; stop = data[1];

回答by user1040975

Maybe this is what you're looking for:

也许这就是你要找的:

int array[] = {1,2};

Java array assignment (multiple values)

Java 数组赋值(多个值)

If you're looking to explicitly assign to each element, I don't think you can do that within one assignment, as a similar concept with the 2-d example below. Which seems like what you want as Jeremy's answers specifies.

如果您希望显式分配给每个元素,我认为您不能在一次分配中做到这一点,就像下面 2-d 示例中的类似概念一样。正如杰里米的答案所指定的那样,这似乎是您想要的。

Explicitly assigning values to a 2D Array?

显式地为二维数组赋值?

回答by SamTebbs33

When declaring several variables of the same type, you can do the following:

在声明多个相同类型的变量时,您可以执行以下操作:

int a = 1, b = 2, c = 3; //etc.

回答by ZhongYu

Maybe

或许

public class PCT 
{
    final Point pos;  // two ints!

    public PCT (File file) 
    {
        pos = parseFile(file);
    }

    public int[] parseFile(File f) 
    {
        Point aa = new Point();
        // ....
        // ....
        return aa;
    }
}