refined 2D design question

Discussion in 'Java' started by Jeff, Dec 21, 2004.

  1. Jeff

    Jeff Guest

    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
     
    Jeff, Dec 21, 2004
    #1
    1. Advertising

  2. Jeff

    Chris Smith Guest

    Jeff <> 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
     
    Chris Smith, Dec 27, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dave
    Replies:
    5
    Views:
    596
    William Brogden
    Jul 17, 2004
  2. Replies:
    3
    Views:
    396
    Daniel T.
    Oct 12, 2006
  3. mmoski
    Replies:
    7
    Views:
    355
  4. DZantow
    Replies:
    0
    Views:
    200
    DZantow
    Dec 20, 2011
  5. Hon Guin Lee - Web Producer - SMI Marketing

    LWP::Simple get() refined problem

    Hon Guin Lee - Web Producer - SMI Marketing, Sep 29, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    139
    Bart Lateur
    Sep 30, 2003
Loading...

Share This Page