0

Let's say that the data type I insert into BST is TypeT. I want to create a BST that can works similarly with either String and Integer data types. I tried to convert TypeT into String and then I used compareTo method in the String class.

But compareTo method doesn't works as I expected for example;

When I run this line ---> System.out.println("5".compareTo("10"));

It outputs 4,what I was expecting was -1 because 5<10

Musahan17
  • 17
  • 1
  • 6
  • "I want to create a BST that can works similarly with either String and Integer data types" Then you shouldn't convert everything to String. Using `String.compareTo` with on integers is not going to work. How about constraining `T` to `Comparable`? Can you show the definition of the BST class and where you are using `compareTo`? – Sweeper Jul 06 '20 at 11:09
  • And for your question, yes compareTo returns positive and negative values during comparison (not just bounded by -1 to +1). When you compare two char sequence their char values are tested. 5 -> 53, 1-> 49 and hence their diff will be 4 – Sabareesh Muralidharan Jul 06 '20 at 11:14
  • Thanks for the answers.I must work on OOP principles before trying to implement BST. – Musahan17 Jul 06 '20 at 11:19

0 Answers0