> I would like to be able to add objects together or subtract one from
> another using simple statements such as:
[quoted text clipped - 5 lines]
> How would I do that? Would it be a lot simpler to just write a
> constructor that has the 2 objects and the operator as arguments?
Since you can't overload operators in Java, you are left with these
kinds of solutions:
MyClass m = m1.add(m2);
or:
MyClass m = MyClass.add(m1,m2);
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
stocksami@earthlink.net - 05 May 2006 15:54 GMT
> > I would like to be able to add objects together or subtract one from
> > another using simple statements such as:
[quoted text clipped - 18 lines]
>
> --
Thanks, I didn't know that operators couldn't be overloaded.
Clark
>I am developing a java application that has some classes that contain
> arrays of numerical data that have multiple dimensions. I would like
[quoted text clipped - 7 lines]
> How would I do that? Would it be a lot simpler to just write a
> constructor that has the 2 objects and the operator as arguments?
You can do it via the constructor, but that implies that every operation
automatically creates new objects and it ties up the constructor logic doing
an implicit operation. A cleaner and simpler solution is to do
MyClass temp = new MyClass ();
temp.set (m1);
temp.add (m2);
This means you only create new objects when you want to preserve the
original parameters. I prefer this technique for long chains of operations
and it leaves you open to create all new kinds of operations. (Matrix /
Complex math is often done this way.) You can combine the constructor and
set with something like
MyClass temp = new MyClass (m1);
or
MyClass temp = m1.copy ();
But the effect is largely the same.
Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
stocksami@earthlink.net - 05 May 2006 16:34 GMT
> >I am developing a java application that has some classes that contain
> > arrays of numerical data that have multiple dimensions. I would like
[quoted text clipped - 30 lines]
> Cheers,
> Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
For this particular case I have an entire flatfile DB loaded into
memory and the enduser will be using Matlab to arbitrarily perform
calculations. The original data has to remain unchanged. Matlab has
the marvelous ability to access object data and method results in a
running java application.
I'll keep your comments in mind when I'm writing the calulation
intensive part of the application, though. I'll be doing more of that
later.
Thanks,
Clark