Android Function to return an array directly into variables

10,308

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};
}
Share:
10,308
ilomambo
Author by

ilomambo

I have a solid programming education. Trying to learn how everything works and interacts. Hopefully, to write great applications.

Updated on June 13, 2022

Comments

  • ilomambo
    ilomambo almost 2 years

    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?
    }
    
  • ilomambo
    ilomambo almost 12 years
    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?
  • Sherif elKhatib
    Sherif elKhatib almost 12 years
    I did not mean as an insult to you. I answered. Can you please explain the "other half"?
  • ilomambo
    ilomambo almost 12 years
    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
  • Sherif elKhatib
    Sherif elKhatib almost 12 years
    unfortunately the other part is not doable ):