Solved

I need to compare a vector of vectors using the Comparable interface.

Posted on 2011-09-26
8
291 Views
Last Modified: 2012-05-12
Here is my code so far:

 public static void ascendingVectorSort(Vector objList, int column)
    {
        for (int i = 1; i < objList.size(); i++) {
            Object element = ((Vector)objList.elementAt(i)).elementAt(column);
            Comparable firstUnsorted = (Comparable)element;
            int index = i - 1;
           
            Object element2 = ((Vector)objList.elementAt(index)).elementAt(column);
            while (index >= 0 && (((Comparable)(element2)).compareTo(firstUnsorted)) > 0) {
                objList.setElementAt((objList.elementAt(index)), (index + 1));
                index--;
            }
            objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));
        }
    }

The goal is using a JTable with a list of a list of items. You use the table to view the directory on your computer. I have everything figured out except for a couple of lines of code. I figured out how to sort a regular vector of objects using comparable but I can't figure out how to sort the vector with a list of vectors in it. I cannot use the Collections.sort() method and I must use the Comparable interface as you see above. This is for a java program.

These two lines I think are the problem:

objList.setElementAt((objList.elementAt(index)), (index + 1));

objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));

Thanks!
0
Comment
Question by:re8622
  • 3
  • 3
8 Comments
 
LVL 9

Expert Comment

by:oheil
ID: 36708582
This may be a problem:
class Vector does not implement interface Comparable, you must implement it:
http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html

And you should not change the input list objList during the loop.
Make a copy of the input list and change the copy. After the loop set the original input list to the new ordered copy.

Oli
0
 

Author Comment

by:re8622
ID: 36711044
I've been able to compare the element that I need in the vector of vectors. The problem I'm having is that it doesn't insert the the information correctly. It takes the first one and then inserts it into each vector resulting in a vector of vectors of the same item. This particular line:

objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));

Is the problem, but I'm not quite sure what to do to fix it. I've compared it to a code that I wrote for just a vector of objects and I don't have this problem. I'm not quite sure how copying the original list into a new list would solve the problem since it doesn't insert properly to begin with.

Thanks!
0
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 36715997
Yes you are right:
t takes the first one and then inserts it into each vector resulting in a vector of vectors of the same item.

Thats what your code is doing.
imagine i = 1
than index = 0
The while loop runs exactly one time and puts the 0th element into 1th element.
After this the 1th element (which is now a copy of 0th element) is put into 0th element.

Therefore the first step i=1 sets the 0th and the 1th element equal to the 0th element.

Thats what you do but probably not intentional.

I suggest something like that:

 public static void ascendingVectorSort(Vector objList, int column)
    {
        int n = objList.size();
        do {
                int something_done=0;
                for (int i = 0; i < n -1; i++) {
                        Object element1 = ((Vector)objList.elementAt(i)).elementAt(column);
                        Object element2 = ((Vector)objList.elementAt(i+1)).elementAt(column);
                        // if( element1 > element2 ) 
                        if( (((Comparable)(element2)).compareTo(element1))>0 ) {
                                objList.setElementAt(element2, i);
                                objList.setElementAt(element1, i+1);
                                something_done=1;
                        }
                }
                n--;
        } while(something_done==1 && n > 1);
    }

Open in new window


I didnt tested and there probably are some syntax errors, but its bubblesort.

Oli


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:re8622
ID: 36718127
Thank you! This is great. I unfortunately need to figure out the sort method using either Selection Sort, Insertion Sort, Merge Sort, or Quick Sort. I'm sorry that I didn't specify this earlier.

I also ran the code and it still doesn't quite sort it. I had to alter it a bit so here it is:

public static void ascendingVectorSort(Vector objList, int column)
    {
        int something_done;
        int n = objList.size();
        do {
            something_done = 0;
            for (int i = 0; i < n - 1; i++) {
                Object element1 = ((Vector)objList.elementAt(i)).elementAt(column);
                Object element2 = ((Vector)objList.elementAt(i + 1)).elementAt(column);
                // if( element1 > element2 )
                if( (((Comparable)(element2)).compareTo(element1))>0 ) {
                    objList.setElementAt(((Vector)objList.elementAt(i + 1)), i);
                    objList.setElementAt(((Vector)objList.elementAt(i)), i + 1);
                    something_done=1;
                }
            }
            n--;
        } while(something_done==1 && n > 1);
    }

This is the output:

 re8622-505364.flv
0
 
LVL 9

Assisted Solution

by:oheil
oheil earned 500 total points
ID: 36813517
So, I would recommend:
<http://en.wikipedia.org/wiki/Quicksort>
implementing is straight foward.

Oli
0
 

Author Comment

by:re8622
ID: 36814463
Thank you.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 37068366
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
lucky13 challenge 11 120
word0 challenge 4 64
groovy example issue 10 88
Support for Notepad++ (including downloading & installing a plugin) 5 61
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now