Java 使用compareTo(String)get时出错,表示类型字符串未定义

Java 使用compareTo(String)get时出错,表示类型字符串未定义,java,string,tree,compareto,Java,String,Tree,Compareto,我正在使用Eclipse,红色下划线显示 类型字符串的方法compareToString未定义 。我怎样才能解决这个问题 树映射代码。。关于节点 public node add(String newWord, int ln, node cur) { if (cur == null) { return new node(newWord,ln); } int result=newWord.compareTo(cur.word); //

我正在使用Eclipse,红色下划线显示

类型字符串的方法compareToString未定义

。我怎样才能解决这个问题

树映射代码。。关于节点

public node add(String newWord, int ln, node cur) {
       if (cur == null) {
           return new node(newWord,ln);
       }

       int result=newWord.compareTo(cur.word); //compareTo is underlined suggesting an error


       if ( result == 0) ;
       else if (result < 0) {
           cur.left = add(newWord,ln, cur.left);
       } else {
           cur.right = add(newWord,ln, cur.right);
       }
       return cur;
   }

   public void add(String word, int ln) {
       root = add(word,ln, root);
   }
这是我使用javac时显示的内容:

错误:找不到符号

int result=newWord.compareTotest

符号:方法比较

位置:字符串类型的变量newWord

其中字符串是类型变量:

字符串扩展类treemap中声明的对象


我试着用一个字符串hello来代替newWord,然后下划线消失了。但是我如何解决这个问题呢?

您可能需要使用equal。以下是一个例子:

class treemap<String, Integer> {

   private class node {
      String word;
      int line;
      node left;
      node right;

      node(String wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }

您可能希望使用equal。以下是一个例子:

class treemap<String, Integer> {

   private class node {
      String word;
      int line;
      node left;
      node right;

      node(String wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }

乍一看,参数'cur.word'导致了这种情况。尝试用test临时替换它,以查看编译错误是否继续


我认为cur.word是不可访问的

乍一看,参数'cur.word'导致了这种情况。尝试用test临时替换它,以查看编译错误是否继续

我认为cur.word是不可访问的

这里String不是java.lang.String,它是用treemap类的定义声明的类型参数。 您正在一个泛型类中工作,String是该类的类型变量

换言之,这:

String one = "Dog";
String two = "dog";
if (one.equalsIgnoreCase(two)) {
  System.out.println("Words are the same");
} else {
  System.out.println("Words are not the same");
}

if (one.equals(two)) {
    System.out.println("Words are the same- Caste Sensative");
} else {
    System.out.println("Words are not the same -- Caste Sensative ");
}
…具有以下相同含义:

class treemap<String, Integer> {

   private class node {
      String word;
      int line;
      node left;
      node right;

      node(String wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }
如果您希望T扩展字符串,U扩展整数,那么您应该这样定义您的类:

class treemap<T, U> {

   private class node {
      T word;
      int line;
      node left;
      node right;

      node(T wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }
这里的String不是java.lang.String,它是用treemap类的定义声明的类型参数。 您正在一个泛型类中工作,String是该类的类型变量

换言之,这:

String one = "Dog";
String two = "dog";
if (one.equalsIgnoreCase(two)) {
  System.out.println("Words are the same");
} else {
  System.out.println("Words are not the same");
}

if (one.equals(two)) {
    System.out.println("Words are the same- Caste Sensative");
} else {
    System.out.println("Words are not the same -- Caste Sensative ");
}
…具有以下相同含义:

class treemap<String, Integer> {

   private class node {
      String word;
      int line;
      node left;
      node right;

      node(String wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }
如果您希望T扩展字符串,U扩展整数,那么您应该这样定义您的类:

class treemap<T, U> {

   private class node {
      T word;
      int line;
      node left;
      node right;

      node(T wd, int ln){
          word=wd;
          line=ln;
      }
   }
   private node root;

   public treemap(){
       root=null;
   }


但我不想用相等的。。我想对单词进行排序,但我不想使用equal。。我想对单词进行排序这都在同一个类中吗?是的,它们在同一个类中这很奇怪,因为String肯定有一个compareToString方法。你试过用javac从命令行编译吗?@MikeChristensen:没有。如果它是私有的,它会工作吗?@JoeAttardi:我试过javac,它说找不到符号。位置:String类型的变量newWord这都在同一个类中吗?是的,它们在同一个类中这很奇怪,因为String肯定有一个compareToString方法。你试过用javac从命令行编译吗?@MikeChristensen:没有。如果它是私有的,它会工作吗?@JoeAttardi:我试过javac,它说找不到符号。位置:StringI test=cur.word和test=cur和compareTotest.word类型的变量newWord,它不工作。这就是你要我做的吗?用hello替换cur.word。这将探测参数是否导致它。首先,在cur.word所在的位置输入一个手动字符串。喜欢考试。看看会发生什么,让我们知道。@iamthereplicant:我试图用test替换新词,下划线消失了。但我不知道怎么解决这个问题。。帮助?从类声明中删除泛型。我尝试了test=cur.word,test=cur和compareTotest.word,但它不起作用。这就是你要我做的吗?用hello替换cur.word。这将探测参数是否导致它。首先,在cur.word所在的位置输入一个手动字符串。喜欢考试。看看会发生什么,让我们知道。@iamthereplicant:我试图用test替换新词,下划线消失了。但我不知道怎么解决这个问题。。帮助?从类声明中删除泛型。