Solved

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

Posted on 2011-09-26
8
283 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:re8622
Comment Utility
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
Comment Utility
So, I would recommend:
<http://en.wikipedia.org/wiki/Quicksort>
implementing is straight foward.

Oli
0
 

Author Comment

by:re8622
Comment Utility
Thank you.
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to implement Singleton Design Pattern in Java.

772 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

10 Experts available now in Live!

Get 1:1 Help Now