1

I'm trying to make iterators for a class called Ship. This is for a genetic algorithm example.

The two key attributes of Ship are weight and volume (knapsack problem). I want one iterator sorting by weight, and one by volume.

I am having trouble setting up the implements Comparable<(?)> interface for the Ship class.

At first I tried to pass a Class called Value which has an enum representing its type and a float value. I have a CompareTo method that looks like this:

return int CompareTo(Value arg0)

 switch (arg0.type())

 case WEIGHT:
  return this._weight.compareTo(arg0.value);

...
seaotternerd
  • 6,298
  • 2
  • 47
  • 58
LostMinty
  • 53
  • 3
  • See example available on [internet](http://www.roseindia.net/answers/viewqa/Java-Beginners/26582-Java-Comparable-Example.html) – MaxZoom Jun 09 '15 at 02:23
  • The example you gave was for using the Comparable (which I exampled there, but as the question went I was looking to have two of these method. If the admins can show a duplicate of my question then great. But ultimately I just got cookie cuttered because I didn't approach in the most formal of way. – LostMinty Jun 09 '15 at 12:55

1 Answers1

1

You'd be better of having two comparators one comparing based on weight and one based on volume and the use those comparators to sort on demand.

Praba
  • 1,373
  • 10
  • 30
  • Thanks you are 100% correct. I was outlining the code I had got stuck on. I was using the comparable derived comparator but instead I can create a new one with the Ship object and the enum – LostMinty Jun 09 '15 at 12:51