原始类型警告(更新)

时间:2016-07-21 11:31:18

标签: java arrays generics

更新:我看到我的问题是重复的,我的问题是:在数组中使用泛型是不安全的。但是,我如何更改代码来修复此问题?我所有的一切都是(我甚至不知道这段代码是否与旧版相似)

   function required() {


 var empt = document.forms["me_validate"]["insert_vender_emails"].value;


if (empt == "") {
alert("Please fill out the vendor email field");
return false;


}

}

但是这段代码也给了我一个警告。

上一篇文章(Raw types warning):

我不是一名java开发人员,但我希望在这部分代码中修复项目中唯一的警告:

_readWriteThreads = (ReadWriteThread<T, RP, SP>[]) Array.newInstance(ReadWriteThread.class, sc.getSelectorThreadCount());

警告:

public abstract class SelectorThread<T extends MMOConnection<T, RP, SP>, RP extends ReceivablePacket<T, RP, SP>, SP extends SendablePacket<T, RP, SP>>
{
    protected static final Log _log = new MMOLogger(SelectorThread.class, 1000);

    private final AcceptorThread<T, RP, SP> _acceptorThread;
    private final ReadWriteThread<T, RP, SP>[] _readWriteThreads;


    protected SelectorThread(SelectorConfig sc, IPacketHandler<T, RP, SP> packetHandler) throws IOException
    {
        _acceptorThread = new AcceptorThread<T, RP, SP>("AcceptorThread", this, sc);
        _readWriteThreads = new ReadWriteThread[sc.getSelectorThreadCount()];

        for (int i = 0; i < _readWriteThreads.length; i++)
            _readWriteThreads[i] = new ReadWriteThread<T, RP, SP>("ReadWriteThread-" + (i + 1), this, sc, packetHandler);
    }

    ...
}

拜托,有人可以帮我解决这个问题,或者告诉我必须知道解决这个问题。我理解上面的行由Type safety: The expression of type ReadWriteThread[] needs unchecked conversion to conform to ReadWriteThread<T,RP,SP>[] 修复,但这不适用于数组。

1 个答案:

答案 0 :(得分:0)

阵列和泛型不能很好地结合在一起。你可以让它们起作用,但这需要付出很多努力。

更好的解决方案是使用List,可能是ArrayList,其中泛型“只是工作”:

private final List<ReadWriteThread<T, RP, SP>> _readWriteThreads;

使用

初始化它
_readWriteThreads = new ArrayList<>();

然后

_readWriteThreads.add(
    new ReadWriteThread<T, RP, SP>("ReadWriteThread-" + (i + 1), this, sc, packetHandler));