Kann mir jemand mit einem Beispiel sagen, was genau die Operatoren : und ? bei JAVA tun?
public static void main(String [] argv){
String a = new String("Hallo");
String b = new String("Hello");
String c = new String("Hello");
IO.println(a.compareTo(b)); // liefert < 0
IO.println(b.compareTo(a)); // liefert > 0
IO.println(b.compareTo(c)); // liefert 0
}
}
Steht auch in der Doku: String (Java Platform SE 6)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string.
f.compareTo(m) == 0 ?
fahrrad : f.compareTo(l) == 0 ?
fahrrad : m.compareTo(l) == 0 ?
meter : f.compareTo(m) == 1 ?
m.compareTo(l) == 1 ?
meter : f.compareTo(l) == 1 ?
lenkrad : fahrrad : m.compareTo(l) == -1 ?
meter : f.compareTo(l) == 1 ?
fahrrad : lenkrad;
}
Danke. Eine weitere Frage wäre wie ich aus Bedingungen ein IF-Lösung machen kann.
Habe das oben bei euren Links gesehen, verstehe es zwar, jedoch fällt mir es schwer, das auf das folgende Beispiel anzuwenden, weil mich die ganzen compareTo's verwirren:
Beispiel:
Java:f.compareTo(m) == 0 ? fahrrad : f.compareTo(l) == 0 ? fahrrad : m.compareTo(l) == 0 ? meter : f.compareTo(m) == 1 ? m.compareTo(l) == 1 ? meter : f.compareTo(l) == 1 ? lenkrad : fahrrad : m.compareTo(l) == -1 ? meter : f.compareTo(l) == 1 ? fahrrad : lenkrad; }
Wie kann ich das in eine IF-Lösung umwandeln:?
Mein Weg: (Scheint mir aber falsch zu sein)
if(f.compareTo(l) == 0){
f = fahrrad;
}
else if(f.compareTo(l) == 0)
f = fahrrad;
else if(m.compareTo(l) == 0)
m = meter;
else if(f.compareTo(m) == 1) ...
^^ need help![]()
private static <T extends Comparable <T>> int blablabla( T[] table, int fahrrad, int lenkrad ){
{
int meter = (lenkrad - fahrrad) / 2;
T f = table[fahrrad], m = table[meter ], l = table[lenkrad];
das mit den wörtern wie fahhrad usw nicht beachten ^^ habe sie einfach so eingesetzt, als beispiel![]()
...f.compareTo(l) == 1 ? lenkrad : fahrrad : m.compareTo(l) == -1 ?...
f.compareTo(m) == 0 ?
fahrrad : f.compareTo(l) == 0 ?
fahrrad : m.compareTo(l) == 0 ?
meter : f.compareTo(m) == 1 ?
....
if(f.compareTo(m) == 0
{
fahrrad;
}
else
{
if(f.compareTo(l) == 0 )
{
fahrrad;
}
else
{
....
}
}
... meter : f.compareTo(m) == 1 ?
m.compareTo(l) == 1 ?
...