scala中的弃用警告

PRA*_*NTH 3 scala

我已经定义了一个名为的函数,ti但在编译之后它显示了一个弃​​用警告.我在Eclipse中做了同样的事情.代码工作正常但它显示警告.

scala>  def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)] = {
     |    if(chars.length!=0) {
     |      var j = 1
     |      var c = chars.head
     |      for(i<-chars.tail) {
     |        if(c==i) j=j+1
     |      }
     |      a::List((c,j))
     |      ti(chars.tail,a)
     |   }
     |   else a
     | }
Run Code Online (Sandbox Code Playgroud)

警告:有3个弃用警告; 使用-deprecation重新运行以获取详细信息ti:(chars:List [Char],a:List [(Char,Integer)])List [(Char,Integer)]

这是什么原因?

Kim*_*bel 8

如果它告诉你用-deprecation重新运行,你应该这样做:

k@k:~$ scala -deprecation
Welcome to Scala version 2.9.1 (OpenJDK 64-Bit Server VM, Java 1.6.0_24).
Type in expressions to have them evaluated.
Type :help for more information.

scala> def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
     |         { if(chars.length!=0)
     |            {
     |            var j=1
     |            var c=chars.head
     |            for(i<-chars.tail)
     |            {
     |            if(c==i) j=j+1
     |            }
     |            a::List((c,j))
     |            ti(chars.tail,a)
     |            }
     |         else a
     |         }
Run Code Online (Sandbox Code Playgroud)

然后你收到警告:

<console>:7: warning: type Integer is deprecated: use java.lang.Integer instead
       def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
                                 ^
<console>:7: warning: type Integer is deprecated: use java.lang.Integer instead
       def ti(chars:List[Char],a:List[(Char,Integer)]):List[(Char,Integer)]=
                                                       ^
<console>:16: warning: type Integer is deprecated: use java.lang.Integer instead
                  a::List((c,j))
                   ^
Run Code Online (Sandbox Code Playgroud)

这里的问题是你应该使用java.lang.Integer或者只是Int不需要与Java的互操作性.所以,你要么import java.lang.Integer或更改IntegerInt.