How to define several variables in one expression

In Python, I can define two variables with an array on the same line.

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

How do I do the same in Java?

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

 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; } } 
+5
source share
6 answers

This is not possible, but you also do not need to call parseFile twice.

Write your code as follows:

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

Python (I believe) supports multiple return values. Java is subject to C conventions and therefore does not allow this. Since this is not part of the language, the syntax for it is also not that it requires a few crude hacks, such as the temp array, if you make multiple returns.

+4
source

You can define several variables as follows:

 double a,b,c; 

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

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

Another aspect is that when preparing a general type variable on the same line, and then from the right assigned variables, you can assign the variable to the left, for example:

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

Having noticed, you can also practice arithmetic operations on a variable, remaining in one line.

+4
source

If you literally mean string ; while you put a semicolon between two statements, they are executed as if there was a new line between them, so you can call:

 a = 1; b = 2; 

You can even compress the entire file in oneliner by deleting the comment (this is the scope to the end of the line). An interval (space, tab, new line, ...) is usually removed from Java files (in memory) as the first step in the Java compiler.

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

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

Perhaps this is what you are looking for:

 int array[] = {1,2}; 

Java array assignment (multiple values)

If you want to explicitly assign to each element, I donโ€™t think that you can do this in one task, as a similar concept with the example of the 2nd example below. It seems like you want, as Jeremy's answers indicate.

Explicit assignment of values โ€‹โ€‹to a 2D array?

0
source

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

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

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

All Articles