ArrayList.set 提示 第二个参数 element 类型是 NoThing ? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
coderstory
V2EX    Kotlin

ArrayList.set 提示 第二个参数 element 类型是 NoThing ?

  •  
  •   coderstory 2022-10-08 11:27:41 +08:00 2737 次点击
    这是一个创建于 1167 天前的主题,其中的信息可能已经有所发展或是发生改变。
     fun setValue(variables: Map<String, Any>, value: Any?) { val instance = operad.eval(variables) ?: throw NullPointerException("instance is null") if (instance is LogicList<*>) { instance.set(property.key.replace("[", "").replace("]", "").trim().toInt(), value) } else { throw IllegalArgumentException("变量 ${(operand as Variable).key} 不是数组") } } 

    代码如上 instance.set 方法 idea 提示我第二个参数 element 是 NoThing 类型 导致我 set 方法无法调用

    Kotlin: Type mismatch: inferred type is Any? but Nothing was expected 

    直接编译 报了如上错误 instance 本身是 Any?类型的,由于做了 null 校验,所以是 Any 类型的。 LogicList 是 ArrayList 的扩展类,set 方法又 List 提供的。 这边我不太清楚要怎么改了,我本身也不太懂 kotlin 。

    6 条回复    2022-10-08 14:30:23 +08:00
    yazinnnn
        1
    yazinnnn  
       2022-10-08 13:42:36 +08:00
    class LogicList:ArrayList<Any?>()
    coderstory
        2
    coderstory  
    OP
       2022-10-08 13:50:52 +08:00
    @yazinnnn

    class LogicList<E> : ArrayList<E> {

    原本是这样的 E 改成 Any 一样的
    coderstory
        3
    coderstory  
    OP
       2022-10-08 14:00:01 +08:00
    @yazinnnn



    class LogicList<E> : ArrayList<E> {

    constructor()
    constructor(collection: Collection<E>) : super(collection)

    var currentRowNumber = -1L
    val current: E?
    get() {
    return if (size != 0) {
    get((if (currentRowNumber == -1L) 0L else currentRowNumber).toInt())
    } else {
    null
    }
    }
    val length: Long get() = size.toLong()
    val empty: Boolean get() = isEmpty()

    @JvmField
    val List = this

    fun next() = currentRowNumber++


    fun reset() {
    currentRowNumber = -1L
    }
    }
    coderstory
        4
    coderstory  
    OP
       2022-10-08 14:06:42 +08:00
    简化一下

    fun setValue( ) {
    val instance: Any = aaa();

    val a: List<*> = instance as List<*>;

    if(instance is MutableList<*>){
    instance.set(1,2) // 在这里 第二个参数 element 的类型提示是 Nothing 导致无法编译
    }
    }

    fun aaa():Any{
    return mutableListOf(1,2,3);
    }
    yazinnnn
        5
    yazinnnn  
       2022-10-08 14:12:16 +08:00
    LogicList 不要带泛型,继承 ArrayList<Any?>()
    Leviathann
        6
    Leviathann  
       2022-10-08 14:30:23 +08:00
    kotlin 和 java 的泛型没区别
    这里用 is 判断以后必须手动转型
    (instance as LogicList<Any?>)[property.key....] = value
    并且 suppress UNCHECKED_CAST
    jvm 上泛型的尽头就是 @Suppress("UNCHECKED_CAST")

    另外这里的 [ 和 ] 如果是前缀和后缀的话可以直接改用 removeSurrounding
    还有第一个 throw 和!!没区别,没附加任何有用的上下文信息,建议把 variable 打印一下
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1175 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 17:49 PVG 01:49 LAX 09:49 JFK 12:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86