0
Analyze the below code and invoke the method using main method. Please help me with this problem.
public int compareTo(String anotherString) { int len1 = value.length; int le2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; whlie (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
7 odpowiedzi
+ 3
Prashant Pal This code seems incomplete and wrong because there is no any string with value and what is anotherString.value.length. We get length of string but what is here anotherString.value?
+ 1
Not getting you all.
+ 1
No.. i need to invoke this method using main method and generate same output as built-in method compareTo() gives.
+ 1
whlie was just typing mistake by me.😅
0
The provided code is exactly the source code of the method compareTo implemented inside the String class: see for example http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java at line 1137
A simple example which invokes this method is here:
https://code.sololearn.com/cHK5GbJV9jQ1
0
Prashant Pal Share your complete Code.
0
I think he has no code at all, just a quiz in some test to explain the use of compareTo method in String class.
By the way there is a typo during the copy of the method: 'whlie' instead of 'while'