2017-03-23 4 views
1

Ich habe Grundkenntnisse, dass Java compareTo Methode lexicographically compares zwei Strings. Ich habe Grundlagen lesen Sie hier String Comparison in JavaJava compareTo Schritt für Schritt Ausführung

Ich habe folgendes Beispiel:

public class CompareTo { 
    public static void main(String args[]) { 
     String str1 = "String"; 
     String str2 = "compareTo"; 
     String str3 = "String"; 
     int var1 = str1.compareTo(str2); 
     System.out.println("str1 & str2 comparison: "+var1); 

     int var2 = str1.compareTo(str3); 
     System.out.println("str1 & str3 comparison: "+var2); 
    } 
} 

ich var1 = -16 und var2 = 0.

Wenn mir jemand diese Lexicographic comparison Schritt für Schritt erklärt, wäre es eine große Hilfe.

Danke.

+3

Warum Sie nicht über den Java-Quellcode finden in OpenJDK? –

+1

Mögliches Duplikat von http://stackoverflow.com/questions/4064633/string-comparison-in-java – JRR

+0

Versuchen Sie, einzelne Zeichenfolgen zu vergleichen. Es wird dir helfen, es zu verstehen. – TDG

Antwort

1

Nun, wenn Sie drucken:

System.out.println ('S'-'c'); 

Sie erhalten -16

String ‚s compareTo die beiden zu einer Zeit String s ein Zeichen vergleicht. Das erste Zeichenpaar, das nicht gleich ist (in Ihrem Fall "S" von "String" und "c" von "compareTo"), bestimmt das Ergebnis. Da lexikografisch 'S' vor 'c' steht, gibt der Vergleich einen negativen Wert zurück, was bedeutet, dass "String" vor "compareTo" stehen sollte.

In Ihrem zweiten Vergleich alle Paare von Zeichen gleich sind, so compareTo 0 zurück

+0

so vergleicht ersten Buchstaben nur in diesem Fall, wenn es gleich ist dann würde es gehen zum zweiten Brief. ist es so ? @Eran – rdj7

+0

@rdj Das ist richtig. – Eran

+0

Warum würde es -1 geben, wenn ich "String" und "Strings" vergleichen @Eran – rdj7

1

Vom Java DOC

public int compareTo(String anotherString) 
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. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true. 
This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position k in the two string -- that is, the value: 

this.charAt(k)-anotherString.charAt(k) 

If there is no index position at which they differ, then the shorter string lexicographically precedes the longer string. In this case, compareTo returns the difference of the lengths of the strings -- that is, the value: 

this.length()-anotherString.length() 

In der ASCII-Tabelle c = 99 (dezimal) und S = 83 (dezimal)

dafür: S - c = -16

+0

Gesehen .. es half, danke @ Xavi – rdj7

Verwandte Themen