Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > returning multiple entities from a method

Reply
Thread Tools

returning multiple entities from a method

 
 
Oliver Wong
Guest
Posts: n/a
 
      08-15-2005

"Kenneth P. Turvey" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> BemusedByQM wrote:
>
>> Yes I have been using a List of objects and returning that. All worked
>> very successfully seemingly until I installed the latest java compiler,
>> which now insists that all objects in the list have to be of the same
>> type, whereas the last compiler was cool with it...

>
> I'm not sure where you are getting the error then. Why don't you post a
> snippet of your code and we'll look at it.


I'm relatively confident the exact "error" he's getting is because he's
upgraded to Java 1.5 and has to do with generis. I'm guessing his code looks
like this:

List myResults = new LinkedList();

where the warning has to do with the fact that Sun strongly recommends
you use the generic version of the collection API, so he should change it
to:

List<ArrayOfComplexNumbers> myResults = new
LinkedList<ArrayOfComplexNumbers>();

Except that one of the elements in that list is a boolean, so really he
needs to put:

List<Object> myResults = new LinkedList<Object>();

In another branch of this thread, I pointed out that there are some
design problems with his code, and he should probably spend the time to fix
those rather than continue to just hack away at his current implementation
until it "just works". Especially since he says he is a perfectionist.

- Oliver


 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      08-15-2005
"BemusedByQM" <(E-Mail Removed)> writes:
>So my question is how do I return two 3-dimensional arrays (each holding
>BigDecimal type entries), and a boolean type variable from a java method???


To return with the appropriate static types, a corresponding
class might be defined (as others already wrote).

The following example tries to deal with the general question,
how to return multiple values. "example1" does use Java only,
while "example2" uses an additional preprocessor.

$define RETURN return multi($1)

$define MULTI java.lang.Object[]

public class Main
{
public java.lang.Object[] multi( java.lang.Object ... it )
{ return it; }

public java.lang.Object[] example1()
{ return multi( System.in, System.out ); }

public MULTI example2()
{ RETURN( System.in, System.out ); }

public static void main( final String[] _ )
{ java.lang.System.out.println( "2005-08-16T00:44:26+02:00" ); }}

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      08-16-2005
On Mon, 15 Aug 2005 17:47:07 GMT, "BemusedByQM"
<(E-Mail Removed)> wrote or quoted :

>So my question is how do I return two 3-dimensional arrays (each holding
>BigDecimal type entries), and a boolean type variable from a java method???


The klutz way to do it is

return new Object[] { three1, three3, new Boolean( whether) };

The proper way to do is in define a new Object type:

new class Pair
{
ThreeD first;
ThreeD second;
boolean whether;
// various constructors getters, setters etc.

}
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      08-16-2005
On Mon, 15 Aug 2005 20:13:58 GMT, "Oliver Wong" <(E-Mail Removed)>
wrote or quoted :

>
> I think the big problem actually is that the Java programming language
>wasn't designed in such a way so as to make it easy to write methods which
>return multiple return values.


If you drew a Java program showing data flow, you would see arrows
converging on each method call. Each method has many inputs and only
one output.

If you come from background such as Forth, this seems unduly
restrictive. Why should a method not have multiple outputs?

I saw one way of adding the notion to a Java-like language using
tuples. A tuple is just a group of items, not necessarily the same
type e.g. the inputs to a method. The outputs are handled the same
way.
 
Reply With Quote
 
BemusedByQM
Guest
Posts: n/a
 
      08-16-2005

"Oliver Wong" <(E-Mail Removed)> wrote in message
news:fd8Me.179255$9A2.2319@edtnps89...
>
> "BemusedByQM" <(E-Mail Removed)> wrote in message
> news:Tz7Me.7371$(E-Mail Removed)...
>> Yes one of my programs is a math program, which carries out an 'LU
>> decomposition' upon a matrix which has been supplied by the user (for
>> matrix here read 'array' - a matrix is simply a math term for a 2D array
>> of numbers). The decomposition returns three matrices upon completing
>> the routine - a P-matrix, L-matrix, and a U-matrix.
>> I think I said in an earlier post that the arrays (or matrices) were 3D -
>> they are and the reason they are 3D and not 2 is because they carry
>> 'complex numbers' which require a second field to represent them.
>>
>> Sometimes though, under certain conditions, the P-matrix is not strictly
>> required, so a boolean value is also returned to 'flag' whether the
>> P-matrix is needed or not.
>>
>> So, I am in the position of having to return three 3-dimensional arrays
>> and a boolean.

>
> Okay, so first of all, I recommend you create a class to represent
> complex numbers, something like:
>
> public class ComplexNumber {
> public double i, r;
> }
>
> should work; you might want to add convenience functions (getting the
> square root of a complex number, for example) to the class.
>
> Then you can create a class which represents the results (is there a name
> of the set { P-matrix, L-matrix, U-matrix}?) I'll call the class "result"
> because I don't know the terminology, but maybe you can come up with a
> better name.
>
> public class Result {
> private ComplexNumber[][] pMatrix, lMatrix, uMatrix;
>
> ComplexNumber[][] getPMatrix() {
> return this.pMatrix;
> }
>
> ComplexNumber[][] getLMatrix() {
> return this.lMatrix;
> }
>
> ComplexNumber[][] getUMatrix() {
> return this.uMatrix;
> }
>
> boolean isPMatrixPresent() {
> return pMatrix != null;
> }
> }
>
> The last method is a common idiom in Java. Rather than returning a flag to
> indicate whether or not the 3rd matrix is there, set pMatrix to null. Then
> you can test whether pMatrix is equal to null or not to determine whether
> or not it's there.


Yes, thats a rather elegant way of expressing the whole thing i must say.
thanks for that

>
> - Oliver
>



 
Reply With Quote
 
Jeremy Watts
Guest
Posts: n/a
 
      08-16-2005

"BemusedByQM" <(E-Mail Removed)> wrote in message
news:wo5Me.4324$(E-Mail Removed)...
>
> "Daniel Dyer" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
>> On Mon, 15 Aug 2005 19:19:34 +0100, Vova Reznik <(E-Mail Removed)>
>> wrote:
>>> class DandyerWrapper{
>>> private BigDecimal[][][] array1;
>>> private BigDecimal[][][] array2;
>>> private BigDecimal[][][] array3;
>>> private Boolean booleanName;
>>> }
>>>
>>> add setters and getters and return List<DandyerWrapper>

>>
>> But I won't be offended if you choose not to name it after me.
>>
>> Dan.

>
> thanks for that daniel, most obliged
>
>>
>> --
>> Daniel Dyer
>> http://www.dandyer.co.uk



Hello Daniel,

Hope you dont mind helping me out a bit further. I have defined the class
:-

class returnWrapper{
private BigDecimal[][][] Pmatrix;
private BigDecimal[][][] Lmatrix;
private BigDecimal[][][] Umatrix;
private Boolean LDMexists;
}


But still am a bit confused as to what comes next I need to define
the sizes of the P,L & U matrices, and also the initial state of the
boolean, which is true in this case. So do I do these initialisations
within the class? And if so how are these done?

Bit confused as to 'getters' and 'setters', still a java newbie I'm afraid.
So once the arrays and boolean have been declared within the class wrapper,
how are they then referred to outside of it?


Thanks


>
>



 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      08-16-2005
On Mon, 15 Aug 2005 22:36:00 +0000, Oliver Wong wrote:

> I'm relatively confident the exact "error" he's getting is because he's
> upgraded to Java 1.5 and has to do with generis. I'm guessing his code looks
> like this:
>
> List myResults = new LinkedList();


List <Object> myResults = new LinkedList<Object>();

This should clear up the warning. Now you declare a list of positively
anything, even mixed types.

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-16-2005
Daniel Dyer wrote:
> On Mon, 15 Aug 2005 19:19:34 +0100, Vova Reznik
> <(E-Mail Removed)> wrote:
>> class DandyerWrapper{
>> private BigDecimal[][][] array1;
>> private BigDecimal[][][] array2;
>> private BigDecimal[][][] array3;
>> private Boolean booleanName;
>> }
>>
>> add setters and getters and return List<DandyerWrapper>

>
> But I won't be offended if you choose not to name it after me.
>
> Dan.


Just to throw in some completely different approach: since the method
seems to be doing quite complex stuff what about applying command pattern?
You create a class for this operation that has properties for input
parameters, an execute method and properties for results. That way you
can easily handle complex results.

Kind regards

robert

 
Reply With Quote
 
Jeremy Watts
Guest
Posts: n/a
 
      08-16-2005

"Stefan Schulz" <(E-Mail Removed)> wrote in message
news(E-Mail Removed).. .
> On Mon, 15 Aug 2005 22:36:00 +0000, Oliver Wong wrote:
>
>> I'm relatively confident the exact "error" he's getting is because
>> he's
>> upgraded to Java 1.5 and has to do with generis. I'm guessing his code
>> looks
>> like this:
>>
>> List myResults = new LinkedList();

>
> List <Object> myResults = new LinkedList<Object>();
>
> This should clear up the warning. Now you declare a list of positively
> anything, even mixed types.


Now why didnt I think of that...?? Or anyone else for that matter...
The simplest and most obvious solutions are often the ones that strike you
last....

>
> --
> You can't run away forever,
> But there's nothing wrong with getting a good head start.
> --- Jim Steinman, "Rock and Roll Dreams Come Through"
>
>



 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      08-16-2005
On Tue, 16 Aug 2005 10:02:23 GMT, Jeremy Watts wrote:

> "Stefan Schulz" <(E-Mail Removed)> wrote in message

...
>> List <Object> myResults = new LinkedList<Object>();
>>
>> This should clear up the warning. Now you declare a list of positively
>> anything, even mixed types.

>
> Now why didnt I think of that...?? Or anyone else for that matter...


Others did. They just thought it better not to mention,
at least until you understood that this collection of
data was actually a class in its own right.

(sigh)

--
Andrew Thompson
physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
"Her voice was soft and cool, her eyes were clear and bright, ..but she's
not there" The Zombies 'She's Not There'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method def in method vs method def in block Kyung won Cheon Ruby 0 11-21-2008 08:48 AM
returning none when it should be returning a list? randomtalk@gmail.com Python 11 05-02-2006 10:26 AM
returning multiple arrays from a java method Jeremy Watts Java 3 05-24-2005 06:52 AM
HTML::Entities::encode() returning wrong(?) entities Jim Higson Perl Misc 3 07-25-2004 09:13 PM
Making entities available to multiple XML documents Matthew Burgess XML 3 07-28-2003 11:27 AM



Advertisments