Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python's multiple assignment is fairly powerful in that it can also be used for <em>parallel assignment</em>, like this:</p> <pre><code>(x,y) = (y,x) # Swap x and y </code></pre> <p>There is no equivalent for parallel assignment in Java; you'd have to use a temporary variable:</p> <pre><code>t = x; x = y; y = t; </code></pre> <p>You can assign several variables from expressions in a single line like this:</p> <pre><code>int a = 1, b = 2, c = 3; </code></pre> <p>Or to map from an array, you can do this:</p> <pre><code>int a = array[0], b = array[1], c = array[2]; </code></pre> <p>If this seems too verbose, you can temporarily create a one-letter reference to your array for the assignment:</p> <pre><code>int[] t = array; int a = t[0], b = t[1], c = t[2]; </code></pre> <p>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:</p> <pre><code>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); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload