views:

719

answers:

3

How can I fill up two vectors and combine them with eachother (either index or an object type in class). I guess if I use index to combine them, then the sorting can be handeld with Collections.sort otherwise I have to create a comparator.

On plus we have to code down to the 1.4 convention. No generics pls...

To make your imagination easier; it's a grid which has a sorting vector (rows) and a content vector (with all cols). They need to be filled in a vector and sorted.

Any hints will also be helpful.

A: 

Please let me know if I understand. As input, you're given two vectors. The first is an ordered list of row numbers that reference indices in the second vector. The second vector is a list of somethings. You would like a vector that contains the same objects as the second vector, but sorted in the order described in the first vector.

Assuming that's what you're trying to do, this is how I'd do it:

public class ThisHadBetterNotBeAHomeworkAssignmentYoungMan {

    public Vector orderContent(Vector indices, Vector content) {
       Object[] orderedStuff = new Object[content.size()];
       for( int i=0; i < indices.size(); i++ ) {
          orderedStuff[i] = content.get(((Integer)indices[i]).intVal());
       }
       return new Vector(orderedStuff);
    }
}

//Note that this is pretty rough code, and I have neither executed it nor bothered checking to see if you can pass an array of stuff into a Vector as a constructor, but you get the idea.

//note also that I'm not sure exactly what you're asking and could be entirely wrong.

CaptainAwesomePants
your description on top is almost what I need. Except that the orderVector doesn't need to be same as contentVector. orderVector should have, in my opinion, just two index.
A: 

Here is my approach... might be doing something wrong here... just tell me so:

int lIndex = 0;

 Vector lSortVector = new Vector();
 Vector lColVector = new Vector();

 for (int i = 0; i < comp.getComponentCount(); i++) {
  JComponent lComponent = (JComponent) comp.getComponent(i);
  GridBagConstraints lConstraints = lLayout.getConstraints(lComponent);

  boolean isFirstColumn = lConstraints.gridx == 0;

  if (isFirstColumn)
   lSortVector.add(lConstraints.gridy, new SortVector(lIndex, lConstraints.gridy));

  lColVector.add(lConstraints.gridx, new ColVector(lIndex, lConstraints.gridx, lComponent));

  if (isFirstColumn)
   lIndex++;
 }

 Collections.sort(lSortVector);
 Collections.sort(lColVector);
This is not an answer to your question, so you should edit your question to include the above (and the clarification in the other comment) and remove this message.SO isn't a forum... :-)
PhiLho
A: 

The OO solution is to wrap the objects in both vectors in something that implements the comparing and keeps a pointer to the real object. This way, you can sort both vectors independent of each other and use the wrapper to get at the real object.

This is the "Decorator" design pattern.

Aaron Digulla