returning multiple entities from a method

B

BemusedByQM

Hi,

I have so far successfully managed to return two 3-dimensional arrays, and
one boolean type variable from a java method using lists.

But the newest java compiler now it seems insists that all entries in a list
must be of the same type.

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 list method still works under this new compiler, but it produces a
warning upon compilation.)


Thanks
 
D

Daniel Dyer

Hi,

I have so far successfully managed to return two 3-dimensional arrays,
and
one boolean type variable from a java method using lists.

But the newest java compiler now it seems insists that all entries in a
list
must be of the same type.

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 list method still works under this new compiler, but it produces a
warning upon compilation.)


Thanks

You could declare the list to be of type java.lang.Object, that would
circumvent the warnings but would allow you to put anything, not just
BigDecimal or Boolean in the list.

If you are mixing types like this are you sure that a list is the best
return type? Myabe some kind of wrapper class that encapsulates the two
BigDecimals and the boolean would be better?

Dan.
 
V

Vova Reznik

Daniel said:
You could declare the list to be of type java.lang.Object, that would
circumvent the warnings but would allow you to put anything, not just
BigDecimal or Boolean in the list.

If you are mixing types like this are you sure that a list is the best
return type? Myabe some kind of wrapper class that encapsulates the
two BigDecimals and the boolean would be better?

Dan.
But return should be list of wrappers
 
B

BemusedByQM

Daniel Dyer said:
You could declare the list to be of type java.lang.Object, that would
circumvent the warnings but would allow you to put anything, not just
BigDecimal or Boolean in the list.

If you are mixing types like this are you sure that a list is the best
return type? Myabe some kind of wrapper class that encapsulates the two
BigDecimals and the boolean would be better?

Hi Daniel (again :) I think we met on the other group )

Yes, how would I set up a wrapper class, to store two 3-dimensional
BigDecimal arrays and a boolean type?

Thanks
 
V

Vova Reznik

BemusedByQM said:
You could declare the list to be of type java.lang.Object, that would
circumvent the warnings but would allow you to put anything, not just
BigDecimal or Boolean in the list.

If you are mixing types like this are you sure that a list is the best
return type? Myabe some kind of wrapper class that encapsulates the two
BigDecimals and the boolean would be better?


Hi Daniel (again :) I think we met on the other group )

Yes, how would I set up a wrapper class, to store two 3-dimensional
BigDecimal arrays and a boolean type?

Thanks

class DandyerWrapper{
private BigDecimal[][][] array1;
private BigDecimal[][][] array2;
private BigDecimal[][][] array3;
private Boolean booleanName;
}

add setters and getters and return List<DandyerWrapper>
 
A

Andrew Thompson

I have so far successfully managed to return two 3-dimensional arrays, and
one boolean type variable from a java method using lists.

But the newest java compiler now it seems insists that all entries in a list
must be of the same type.

Very demanding compiler, no? ;-)
So my question is how do I return two 3-dimensional arrays (each holding
BigDecimal type entries), and a boolean type variable

<OOP zen question>
What is the one word that describes the combined
'3D arrays and a boolean'?
</OOP zen question>
 
K

Kenneth P. Turvey

BemusedByQM said:
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???

You're going to have to return a list of Objects.

You could also define a class that contains the values you need and return
an instance of the class. I don't know how the data is related, but this
is probably what you really want.

The message you are getting from your compiler is just a warning. You can
safely ignore it, if you choose to.
 
D

Daniel Dyer

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.
 
B

BemusedByQM

Andrew Thompson said:
Very demanding compiler, no? ;-)

well i guess strongly typed languages like java make for robustness, but
they also can be frustratingly inflexible.... i usually program in PHP, and
returning multiple variables from a PHP function is so trivial as to be
beyond consideration...
 
B

BemusedByQM

Daniel Dyer said:
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 :)
 
A

Andrew Thompson

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

What about the lower case 'd', doesn't *that* offend you?
Do people have no standards? [ ;-) ]
 
V

Vova Reznik

BemusedByQM said:
well i guess strongly typed languages like java make for robustness, but
they also can be frustratingly inflexible.... i usually program in PHP, and
returning multiple variables from a PHP function is so trivial as to be
beyond consideration...
May be you'd better stay with PHP.
It is really not daily situation when you need
to have 3D array(s).
 
B

BemusedByQM

Vova Reznik said:
May be you'd better stay with PHP.
It is really not daily situation when you need
to have 3D array(s).

no its not the fact that the returning of 3D arrays is a problem, its just
if you have to return several things of different type. as i say in php
this is trivial, but javas insistence on type can present problems
 
V

Vova Reznik

BemusedByQM said:
no its not the fact that the returning of 3D arrays is a problem, its just
if you have to return several things of different type. as i say in php
this is trivial, but javas insistence on type can present problems
Guess how its done in PHP.
 
O

Oliver Wong

BemusedByQM said:
no its not the fact that the returning of 3D arrays is a problem, its just
if you have to return several things of different type. as i say in php
this is trivial, but javas insistence on type can present problems

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. You're making a "hack" by returning just one
object (a list) which actually contains all the return values you want, but
this is fighting the language, so to speak.

Normally I wouldn't suggest this, since it's a long term solution, but
you seem to be sticking around these newsgroups, so here goes. Can you give
a grand overview of what your program does? I think there are some
fundamental design problems with it, perhaps because you come from a PHP
background.

I'm not saying that PHP is "bad", but that you have to think differently
in Java than you do in PHP. It's like when an English speaker learns French
and tries to use an expression that doesn't exist in French (e.g. "What's
up?" instead of "What's new?").

Java is more object oriented than PHP, which is why you're receiving a
lot of suggestions to wrap your return values into an object and to return
just that one object.

- Oliver
 
B

BemusedByQM

Oliver Wong said:
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. You're making a "hack" by returning just
one object (a list) which actually contains all the return values you
want, but this is fighting the language, so to speak.

Normally I wouldn't suggest this, since it's a long term solution, but
you seem to be sticking around these newsgroups, so here goes. Can you
give a grand overview of what your program does? I think there are some
fundamental design problems with it, perhaps because you come from a PHP
background.

Hi,

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.

I could simply have used one array I suppose and just returned that (having
combined the three matrices into one single array), but that still leaves
the boolean...!

As for a problem with the design of the program, then I am just following a
standard algorithm to find LU decomps (in fact as far as I know there are
only two and they both produce multiple matrices)

I'm not saying that PHP is "bad", but that you have to think
differently in Java than you do in PHP. It's like when an English speaker
learns French and tries to use an expression that doesn't exist in French
(e.g. "What's up?" instead of "What's new?").

Very true and I see your point - Java is very different to PHP i think and
requires a paradigm shift in thinking.

Java is more object oriented than PHP, which is why you're receiving a
lot of suggestions to wrap your return values into an object and to return
just that one object.

yes I think I will go down the route of using an object wrapper and return
that
 
B

BemusedByQM

Kenneth P. Turvey said:
You're going to have to return a list of Objects.

You could also define a class that contains the values you need and return
an instance of the class. I don't know how the data is related, but this
is probably what you really want.

The message you are getting from your compiler is just a warning. You can
safely ignore it, if you choose to.

Hi,

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 a perfectionist I'm afraid and ignoring the warnings will not do. I
think I will use a class to wrap the objects in, as previous posters have
said
 
K

Kenneth P. Turvey

BemusedByQM said:
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 a perfectionist I'm afraid and ignoring the warnings will not do. I
think I will use a class to wrap the objects in, as previous posters have
said

That really is probably what you want. I suspect that these three values
really represent something or you wouldn't be handing them off together all
the time.
 
K

Kenneth P. Turvey

BemusedByQM said:
As for a problem with the design of the program, then I am just following
a standard algorithm to find LU decomps (in fact as far as I know there
are only two and they both produce multiple matrices)

You just named your new class, LUDecomps. :)

What you are really returning isn't three three dimensional arrays and a
boolean, but one LUDecomp composed of three three dimensional arrays and a
boolean.

You need to create a class for this new beast.
yes I think I will go down the route of using an object wrapper and return
that


Looking into the math here, I see why you are looking at it the way you are.
Breaking a matrix into lower and upper triangular matrices doesn't sound
like creating a new object.

I think the operation you are trying to perform belongs in the matrix class
not in some other class. You might have methods, getLowerTriangularDecomp
and getUpperTriangularDecomp or similar.

A wrapper class might solve the problem today, but I'm not sure that it is a
perfect fit.
 
O

Oliver Wong

BemusedByQM said:
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.

- Oliver
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top