Return two arrays in a method in Java

Viewed 52242

Considering I have two arrays for example:

String[] array1 = new String[10];
int[] array2= new int[10];

So that inside a method I've computed two arrays, namely array1 & array2
and now I want to return both of these arrays. How should I go about it?

I read here that I can make another class and define certain object types and encapsulate these arrays in that class constructor, but I'm still confused and did not understand completely.

If you could show me a working example which does that, or may be any similar idea, it would be good.

10 Answers

Use new Object[]{array1, array2} to retun.

retun new Object[]{array1,array2};

And in caller method you will get returned object as "obj" then get

int[] array1 = obj[0] ;
int[] array2 = obj[1];
Related