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

时间:2023-01-26 15:12:16

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;
    }
}

6 个解决方案

#1


4  

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

这是不可能的,但您也不需要两次调用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 temp array are needed if you're doing multiple returns.

Python(我相信)支持多个返回值。 Java遵循C约定,因此不允许它。由于这不是语言的一部分,因此它的语法也不是,这意味着如果您正在进行多次返回,则需要像temp数组那样略有严重的黑客攻击。

#2


4  

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.

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

#3


2  

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:

如果你文字意味着线;只要在两个语句之间放置一个分号,它们就会被执行,就像它们之间有一条新行一样,你可以调用:

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.

您甚至可以通过删除注释(该范围到行尾)将整个文件压缩到oneliner中。作为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:

但你可能对这个声明更感兴趣。 Sytax喜欢[start,stop] = parseFile(file);不受支持(至少目前不支持)。你可以做一个在线人:

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

#4


0  

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.

如果您希望明确地分配给每个元素,我认为您不能在一个任务中执行此操作,就像下面的二维示例中的类似概念一样。这看起来就像杰里米的答案所指出的那样。

Explicitly assigning values to a 2D Array?

明确地将值分配给2D数组?

#5


0  

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

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

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

#6


0  

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;
    }
}

#1


4  

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

这是不可能的,但您也不需要两次调用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 temp array are needed if you're doing multiple returns.

Python(我相信)支持多个返回值。 Java遵循C约定,因此不允许它。由于这不是语言的一部分,因此它的语法也不是,这意味着如果您正在进行多次返回,则需要像temp数组那样略有严重的黑客攻击。

#2


4  

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.

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

#3


2  

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:

如果你文字意味着线;只要在两个语句之间放置一个分号,它们就会被执行,就像它们之间有一条新行一样,你可以调用:

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.

您甚至可以通过删除注释(该范围到行尾)将整个文件压缩到oneliner中。作为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:

但你可能对这个声明更感兴趣。 Sytax喜欢[start,stop] = parseFile(file);不受支持(至少目前不支持)。你可以做一个在线人:

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

#4


0  

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.

如果您希望明确地分配给每个元素,我认为您不能在一个任务中执行此操作,就像下面的二维示例中的类似概念一样。这看起来就像杰里米的答案所指出的那样。

Explicitly assigning values to a 2D Array?

明确地将值分配给2D数组?

#5


0  

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

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

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

#6


0  

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;
    }
}