3

我定义了一个名为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
     | }

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

这是什么原因?

4

1 回答 1

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
     |         }

然后你得到你的警告:

<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))
                   ^

这里的问题是您应该使用java.lang.Integer或者仅Int在您不需要与 Java 的互操作性时使用。因此,您要么import java.lang.Integer更改IntegerInt.

于 2012-10-21T06:30:20.353 回答