-7

This is my Code and I can't sort my LinkedList.

import java.util.Collections;
import java.util.LinkedList;

import org.omg.CosNaming.NameComponent;

public class Zug implements Comparable<Zug> {

    private String abfahrtzeit;
    private String zuggattung;
    private int zugnummer;
    private int fahrtzeit;

    public Zug(String zeile) {

        String[] teile = zeile.split(";");
        this.abfahrtzeit = teile[0];
        this.zuggattung = teile[1];
        this.zugnummer = Integer.parseInt(teile[2]);
        this.fahrtzeit = Integer.parseInt(teile[3]);

    }

    public String getAbfahrtzeit() {
        return abfahrtzeit;
    }

    public String getZuggattung() {
        return zuggattung;
    }

    public int getZugnummer() {
        return zugnummer;
    }

    public int getFahrtzeit() {
        return fahrtzeit;
    }

    public String toString() {
        return this.abfahrtzeit + ";" + this.zuggattung + ";" + this.zugnummer + ";" + this.fahrtzeit;
    }

  //                          This is the Problem Block

     @Override
    public int compareTo (Zug z) {

        String datei = "Zuege.dat";
        LinkedList<Zug> ll = new LinkedList<Zug>();

        Collections.sort( ll, new NameComponent() );

        ll = getDaten(datei);

        return this.fahrtzeit - z.getFahrtzeit();
    }
// End Of Problem Block

    private LinkedList<Zug> getDaten(String datei) {
        return null;
    }
}   
Robo Mop
  • 3,485
  • 1
  • 10
  • 23
As Pirin
  • 1
  • 1
  • 4
    So, what is the problem? Explain it in more detail than just "I can't sort my linked list" and "this is the problem block". Do you get errors? If yes, then what are the exact error messages? And why are you using class `org.omg.CosNaming.NameComponent`? That doesn't seem to have anything to do with what you're trying to achieve. – Jesper Jun 06 '17 at 12:00
  • How do you create and sort your list? I don't get what you're trying to do in the `compareTo` method with creating new Lists and loading a file in there seems wrong.. the `compareTo` method is only used to compare two objects, you don't actually sort in there. – xander Jun 06 '17 at 12:02
  • `Collecitons.sort()` accepts as 2nd argument `Comparator` not `Comparable`. – diginoise Jun 06 '17 at 12:04
  • 2
    Your `compareTo` method should contain code that compares two elements of type `Zug` - `this` and `z`. It should *not* be sorting collections. Just saying whether `this` is greater than `z`, `this` is less than `z`, or `this` is equal to `z`. Please read the documentation. – RealSkeptic Jun 06 '17 at 12:05
  • It seems you don't know what you are doing :). I will recommend you to read some articles about how `compareTo` works and how it is used. Also look at some resources which explains usage of `Comparator`. – matoni Jun 06 '17 at 12:09

1 Answers1

0
  • As RealSkeptic and matoni write, you must not do anything other in the compareTo(Zug z) method than compare this to z - as the method name implies. compareTo(Zug z) is called by methods sorting a collection whenever they need to compare two elements of that collection. Loading lists of objects in that method doesn't make any sense.

  • The most simple implementation would be

    @Override
    public int compareTo(Zug z) {
        return this.fahrtzeit - z.getFahrtzeit();
    }
    

    You may want to test your code with that implementation. Generate a few example Zug objects, add them to a List, sort that list using Collecitons.sort() and enjoy the result (or give us a meaningful error message).

  • Java Practices has an elaborate example on how to write a compareTo()-method.

  • Please note that this implementation is not consistent with equals() (as detailed in the javadoc).

  • Sorting by fahrzeit might not be the only way to sort your objects and probably should not be the natural order. You probably should implement a Comparator (e.g. FahrzeitComparator, AbfahrtzeitComparator ...) to be able to sort by different criteria. See this example.

Oh, and:

  • Rewriting your code with English variable names would allow more people to understand what your objects should represent...
sruetti
  • 532
  • 2
  • 7