Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   refined 2D design question (http://www.velocityreviews.com/forums/t139436-refined-2d-design-question.html)

 Jeff 12-21-2004 12:03 AM

refined 2D design question

Last Friday, I posted a question about sorting two dimensional arrays which
evoked some great responses on the exact nature of two dimensional arrays in
Java. That discussion made me refine what I was trying to accomplish and
how. Now I'd like to get other opinions on my new approach.

Here's the problem. My input is data that has 4 fields: 1 String, 1 float,
2 ints. The input comes from various other classes - it's not in rows. The
String is the subject, the float and ints describe the subject. A separate
program eventually puts my output in a 2 dimensional Swing table. Up to 1000
rows can arrive as input, but I want to pass the presenting program only the
top 15 rows. The rows with the largest float values are selected for
presentation, thus the need to sort the input.

One other influence. I typically use Vectors of Vectors to implement my
Swing table model.

The best solution I can think of is to extend Vector to implement the
Comparable interface, creating a class called ComparableVector for each row.
In ComparableVector's compareTo() method, I'll compare the float value of
each row. Then I can use the Arrays.sort() method to perform the sort. A
Vector of ComparableVectors will provide the 2nd dimension.

Does someone see a better solution?

--
Jeff

 Chris Smith 12-27-2004 07:41 PM

Re: refined 2D design question

Jeff <jeffdrew@bellatlantic.net> wrote:
> The best solution I can think of is to extend Vector to implement the
> Comparable interface, creating a class called ComparableVector for each row.
> In ComparableVector's compareTo() method, I'll compare the float value of
> each row. Then I can use the Arrays.sort() method to perform the sort. A
> Vector of ComparableVectors will provide the 2nd dimension.
>
> Does someone see a better solution?

Yes. You're entirely missing the concept of abstraction. You should
really have something like this:

public class MyDataRecord implements Comparable<MyDataRecord>
{
private String name;
private float value;
private int i1, i2;

...

public int compareTo(MyDataRecord other)
{
return Float.compare(value, other.value);
}
}

That use a Vector of those. You mentioned that the display of this data
in a Swing table is in another program, and there's nothing wrong with
using different data models for the same data in different programs.
However, if you would like to use a JTable to display the data in this
form, just define a subclass of AbstractTableModel to define a mapping
from a Vector<MyDataRecord> to the TableModel interface. You'll define
what field goes into what column by your implementation of getValueAt
(and probably getColumnName and getColumnClass as well).

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

 All times are GMT. The time now is 04:41 PM.