I'm trying to teach myself the next set of java lessons for my upcoming fall session of class. I'm having difficulty with this bit of code I have. I think the problem starts in the compareTo method, I'm not sure how to write it...then i'm not sure if my insertionsort is even right. any help would be awesome!
This solves one problem of working with Object as the interface requires. It will throw a class cast exception if the other is not an ArrayPlus. That might be what you want ... or not. Have you tried to write the next method?
Do you see why the first method calls the second?
The typical form for a class with multiple fields is something like:
You only have one field, your array. How would you compare two arrays? Just describe the comparison in plain English. See if the Arrays class has anything useful.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Oct 13, 2006
That's the way i'm used to doing it, by comparing fields...all the objects stored in the ArrayPlus class are integers...but what field do I compare them to? I just want to be able to say, is this integer equal to, greater than, or less than this integer? Because even though they are objects, they are integers in the long run. How would I code something like that?
Joined: Jan 29, 2003
Sounds like the problem is just doing compareTo for primitives? The lots of typing way would be:
You could compress that into a couple nested ternary expressions if you have the guts to face the next person who has to read it.
You still need to figure out your definition of one array "greater than" another. Iterate that test through the arrays until result is not 0? Compare the lengths? Sums of all the entries? Compare the smallest entry in each array?