2

I saw that javascript has something similar.
Is it possible to do something like this in Android? I know how to do it with array names, my question is about returning and assigning directly from the variables that compose the arrays.

private int[] calcHead() {
    int i;
    int j;
    int k;
    // Do stuff
    return {i, j, k};     <-- is this possible? (I am getting error: "The Method must return a result of type int[]")
}

private void otherFunc(){
    int a;
    int b;
    int c;

    {a, b, c} = calcHead();   <-- is this possible?
}

2 Answers 2

13

This is a rather basic question: Anyway :

public int[] foo() {
    int i = 0;
    int j = 0;
    int k = 0;
    return new int[]{i,j,k};
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, silly questions are the most difficult to google. Please can you answer the other half of the question (the assignment to multiple variables at once), so I credit you with the answer?
I did not mean as an insult to you. I answered. Can you please explain the "other half"?
No insult taken! This forum is sometimes for silly questions too, if you cannot find the answer reasonably fast by searching yourself. The other half is: can I do something like {a,b,c} = calcHead( ), see in the question body
unfortunately the other part is not doable ):
0

In answer to the second part of the question, instead of

{a, b, c} = calcHead();

you can do

a = b = c = calcHead();

although its not very elegant it would assign the return value to each variable...

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.