refined 2D design question

J

Jeff

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?
 
C

Chris Smith

Jeff said:
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
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top