returning multiple arrays from a java method

J

Jeremy Watts

hi ,

i've already posted this message in another ng, so excuse the cross post.

i've written a method that returns three 3-dimensional arrays, and my
question is to how to return these arrays without putting them in
another array and returning that. someone mentioned the use of lists, which
i have now successfully implemented, so my three arrays now reside in a list
which is then returned by the method.

my problem now seems how to reference the arrays in the list once the list
has been returned? in the case of simple integer values say being in the
list then this doesnt seem to be a problem. if say the returned list was
'result' , then the values can be displayed by
system.out.println(result.get(0)), system.out.println(result.get(1)) etc..

but what if 'result' now itself contains arrays, then how are individual
entries of these arrays accessed? i've tried accessing the array elements
by using 'system.out.println(result.get(0)[0][0][0]), but this produces an
error message saying i cant treat a java object as an array. could someone
please tell me how to access array elements that are placed in a list??

thanks
 
R

Rivky

You have to break it down or downcast it:Something like this:
(Not tested)

((String [][])result.get­(0))[0][0]

Or assuming a String Array:

String [][] temp = (Array)result.get­(0);
temp[0][0];
 
W

Wibble

Actually, you probably want to return a value object.
Define a new class to hold the result and return an instance
of that. Its more efficient than a list, and allows
you to provide some semantics, type safety, compile-time checking
etc.
 
J

Jeremy Watts

You have to break it down or downcast it:Something like this:
(Not tested)

((String [][])result.get­(0))[0][0]

Or assuming a String Array:

hi,
yes that worked fine thanks. was missing a set of brackets in there
somewhere. javas a very fussy language :)

String [][] temp = (Array)result.get­(0);
temp[0][0];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top