carookee - group communication for you
Home / JavaForum / Java allgemein
Infos   |   Features   |   Gold-Edition   |   Kundenservice   
java
  Übersicht
  Forum
Beginner
Java allgemein
JDBC
JNI
Networking
Online-Ressourcen
Swing + AWT
XML
Meckerecke
  Mitglieder
LOGIN





· Passwort vergessen
· Kostenlos anmelden
  Information
  Demo
  Features
  Im Vergleich
  Anmeldung
SUCHE
Beiträge, Foren oder Verfasser finden:
  Kundenservice
  Impressum
  Datenschutz
  AGB
Status
5.165 User online
0 User eingeloggt
 

Beiträge
   Antworten     Neuer Beitrag    

Beitrag 2009 von 2212 (91%) |<   <   >   >|

Autor Chris Lenz
Datum 08.01.02, 05:40
Betreff Re: Vector sortieren


Hab ich mal irgendwo gefunden, im Usenet glaube ich:

---------------------------------------------------------------------
SortableVector.java:
---------------------------------------------------------------------
import java.util.*;

/**
* A simple extension to the java.util.Vector class which provides
* a method to sort the elements.
*/

public class SortableVector
extends Vector
{
    /**
    * Sorts the elements of the vector using a simple O(n^2) selection
    * sort. The comp argument must be an object which implements the
    * ElementComparator interface.
    */

    public void sort(ElementComparator comp)
    {
        int smallest;
        for (int i = 0; i < elementCount - 1; ++i) {
            //find smallest remaining element
            smallest = i;
            for (int j = i + 1; j < elementCount; ++j) {
                if (comp.preceeds(elementData[j], elementData[smallest])) {
                    smallest = j;
                }
            }
            //exchange smallest and i
            if (smallest != i) {
                Object tmp = elementData[i];
                elementData[i] = elementData[smallest];
                elementData[smallest]= tmp;
            }
        }
    }
    
    /**
    * Testing purposes only.
    */

    public static void main(String args[])
    {
        //Create new vector
        SortableVector v = new SortableVector();
        System.out.println("Creating vector...");
        v.addElement("completely unsorted: 3");
        v.addElement("completely unsorted: 5");
        v.addElement("completely unsorted: 2");
        v.addElement("completely unsorted: 1");
        v.addElement("completely unsorted: 4");
        v.addElement("already sorted: 1");
        v.addElement("already sorted: 2");
        v.addElement("already sorted: 3");
        v.addElement("already sorted: 4");
        v.addElement("already sorted: 5");
        v.addElement("descending: 5");
        v.addElement("descending: 4");
        v.addElement("descending: 3");
        v.addElement("descending: 2");
        v.addElement("descending: 1");
        //Sort data
        System.out.println("sorting...");
        v.sort(new ElementComparator() {
            public boolean preceeds(Object element1, Object element2)
            {
                return ((String)element1).compareTo((String)element2) < 0;
            }
        });
        //Output sorted data
        Enumeration e = v.elements();
        while (e.hasMoreElements()) {
            System.out.println(" " + (String)e.nextElement());
        }
    }
}

---------------------------------------------------------------------
ElementComparator.java:
---------------------------------------------------------------------
/**
* This interface implements a single method preceeds which returns
* true if both arguments are in ascending order (i.e. if element1
* preceeds element2). Otherwise it returns false. It is used as an
* argument to the the sort method of the SortableVector class.
*/

public interface ElementComparator
{
    public boolean preceeds(Object element1, Object element2);
}


---------------------------------------------------------------------
Anwendungsbeispiel s. main() in SortableVector.

Chris


Diskussionsverlauf:
    Re: Vector sortieren
Vector sortieren
        Re: Vector sortieren

 Auf diesen Beitrag antworten
 Neuen Beitrag verfassen


|<   <   >   >|

                                                                                                                                                                                                                           

Impressum  · Datenschutz  · AGB  · Infos  · Presse
Ein modernes Forum: teamturn.com