0

So i need to return multiple arrays from a function in process. The problem is i don't know which function value to use? (eg int function, string function etc) Also, how do i return multiple values? I need the send barrierx and barriery from arrayGen() to barriers(). Thanks for your help!

Final:

int barriers(){
    if (R == 1){     
        while (size>0){
            barrierx[size-1] = randomInt(512);
            barriery[size-1] = randomInt(512);    
            size = size - 1;
        }

       while (sizeC>0){
           ellipse( barrierx[sizeC-1],  barriery[sizeC-1], 100, 100);
           sizeC = sizeC - 1;        
       }
       sizeC = sizeB;     
       R = 0;
    }
    return 5;
   }

  int arrayGen(){
      int size = randomInt(11);
      int sizeB = size;
      int sizeC = size;

      int[] barrierx = new int[size];
      int[] barriery = new int[size];
  }

EDIT:

int arrayGen(){

  int size = randomInt(11);
  int sizeB = size;
  int sizeC = size;
  int[] result = new int[2];

  int[] barrierx = new int[size];
  int[] barriery = new int[size];

   while (size>0){
    barrierx[size-1] = randomInt(512);
    barriery[size-1] = randomInt(512);    
    size = size - 1;
  }


  result[0] = barrierx;
  result[1] = barriery;
return result;
}

type mismatch cannot convert from int[] to int

4
  • 1
    if you want to return multiple arrays, return an int[][] ( array of arrays ) Commented Jun 3, 2016 at 11:27
  • You could use return a List Commented Jun 3, 2016 at 11:29
  • Please, read this (how to ask) and this (mcve) before asking, as those will help you get more and better answers from the community. When asking a question, try to be as specific as possible, as well as show what kind of answers/attempts you already made. If we have to guess any information, is likely that the help wont be helpfull.... Welcome to stackoverflow, and good luck onwards. Commented Jun 3, 2016 at 11:31
  • @KevinEsche How would that work? I tried return: int[barrierx][barriery]; but it said error on class? Commented Jun 3, 2016 at 11:33

2 Answers 2

1

Define a class containing all the data fields you need. Then return an object of that class.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't return multiple values from a function, but you can return a single value that wraps them all.

If the values types are homogeneous, use an array or a Collection of that type, as suggested in the comments by Kevin Esche. For example :

int[] barrierx = new int[size];
int[] barriery = new int[size];
return {barrierx, barriery};  // later access barrierx by result[0] and barriery by result[1]

or more explicitely :

int[] barrierx = new int[size];
int[] barriery = new int[size];
int[][] result = new int[size][2];
result[0] = barrierx;
result[1] = barriery;
return result;

As you can see from the type declaration of the returned value, the method signature needs to be int[][].

If the values types are heterogeneous, use a class whose fields contain these values, with the specific types needed, as answered by JF Meier.

9 Comments

Both arrays are full of randomly generating integers. How would i do this?
Does my example makes it clear? Note that the content of the arrays does not matter (altough their size does, since it's part of their "type").
Yes it makes sense to me, but i am getting 'type mismatch cannot convert from int[] to int' on result, barrierx and barriery, see my edit? Thanks. I am using processing if that helps.
In case you haven't seen it, I've answered this problem in a comment on your question : you need to change your method signature since it does not return a single into but rather an array of arrays of ints
Sweet this works great! I also need to return an integer (size) in the same function along with the two arrays. Is this possible?
|

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.