java Java中的并行分配?

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

Parallel assignment in Java?

java

提问by powerboy

Does Java have something similar to Python's [a, b, c] = (1, 2, 3)or PHP's list($a, $b, $c) = array(1, 2, 3)?

Java 有类似于 Python[a, b, c] = (1, 2, 3)或 PHP 的东西list($a, $b, $c) = array(1, 2, 3)吗?

采纳答案by Charlie Martin

Not really. You can do x = y = 0to set several variables, but not a parallel assignment like Python.

并不真地。您可以x = y = 0设置多个变量,但不能像 Python 那样并行赋值。

回答by D Coetzee

Python's multiple assignment is fairly powerful in that it can also be used for parallel assignment, like this:

Python 的多重赋值相当强大,因为它也可以用于并行赋值,如下所示:

(x,y) = (y,x) # Swap x and y

There is no equivalent for parallel assignment in Java; you'd have to use a temporary variable:

Java 中没有并行赋值的等价物;你必须使用一个临时变量:

t = x; x = y; y = t;

You can assign several variables from expressions in a single line like this:

您可以在一行中从表达式中分配多个变量,如下所示:

int a = 1, b = 2, c = 3;

Or to map from an array, you can do this:

或者从数组映射,你可以这样做:

int a = array[0], b = array[1], c = array[2];

If this seems too verbose, you can temporarily create a one-letter reference to your array for the assignment:

如果这看起来太冗长,您可以临时创建一个对数组的单字母引用以进行赋值:

int[] t = array;
int a = t[0], b = t[1], c = t[2];

Getting more to the root of the question, multiple assignment tends to be handy in Python in situations where code is passing around several related variables (perhaps of different types) together in a list or array. In Java (or C/C++), the more idiomatic way to do this would be to create a small data class (or struct) to bundle these variables up together, and have both the producer and consumer use it. You can then refer to the fields by name instead of by index, like this:

更深入地了解问题的根源,在代码在列表或数组中一起传递多个相关变量(可能是不同类型)的情况下,多重赋值在 Python 中往往很方便。在 Java(或 C/C++)中,更惯用的方法是创建一个小的数据类(或结构)来将这些变量捆绑在一起,并让生产者和消费者都使用它。然后,您可以按名称而不是按索引引用字段,如下所示:

class Foo {
    public int a;
    public int b;
    public int c;
}

/* ... */

Foo produceFoo() {
    Foo f = new Foo();
    f.a = 1;
    f.b = 2;
    f.c = 3;
    return f;
}

/* ... */

Foo f = produceFoo();
System.out.println(f.a + "," + f.b + "," + f.c);

This also opens the door to later refactoring that will make Foo a real class with behavior and encapsulated private data, not just a data class.

这也为以后的重构打开了大门,这将使 Foo 成为具有行为和封装私有数据的真正类,而不仅仅是数据类。

回答by Patrick

Parallel assignment wouldn't be difficult to add to Java, we actually implemented it in our OptimJ language extension. But it just isn't there.

将并行赋值添加到 Java 中并不困难,我们实际上在我们的 OptimJ 语言扩展中实现了它。但它只是不存在。

As Derrick mentions, parallel assignment is required for an atomic swap statement.

正如 Derrick 所提到的,原子交换语句需要并行赋值。

What you call parallel assignment is an instance of a more general concept called "destructuring assignment": you have some structure, and you match parts of it to variables.

你所说的并行赋值是一个更普遍的概念的实例,称为“解构赋值”:你有一些结构,你将它的一部分与变量相匹配。

Suppose you have embedded tuples, then destructing assignment can extract data at any level:

假设你已经嵌入了元组,那么析构赋值可以提取任何级别的数据:

(x, (y, z)) = ((1, 2, 3), (4, (5, 6)))
// x = (1, 2, 3)
// y = 4
// z = (5, 6)

Suppose you have a list or a set, then destructuring assignment can extract sublists or subsets (x stands for an element, x* stands for a sublist):

假设你有一个列表或一个集合,那么解构赋值可以提取子列表或子集(x代表一个元素,x*代表一个子列表):

[ x, y*, z ] = [ 1, 2, 3, 4 ]
// x = 1
// y = [ 2, 3 ]
// z = 4

Obviously lists and sets can be embedded with tuples. This simple scheme provides very powerful programming abstractions, useful as soon as you need to extract data.

显然列表和集合可以嵌入元组。这个简单的方案提供了非常强大的编程抽象,在您需要提取数据时非常有用。

回答by aseychell

Try int[] list = {1,2,3}. This creates an integer array with values 1, 2 and 3 respectively.

试试int[] list = {1,2,3}。这将分别创建一个值为 1、2 和 3 的整数数组。