第 27 条:消除非受检警告

2021-05-10  本文已影响0人  综合楼
消除非受检警告.jpeg
加在方法上
    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        if (a.length < size)
            // Make a new array of a's runtime type, but my contents:
            return (T[]) Arrays.copyOf(elementData, size, a.getClass());
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }
加在变量上
     public <T> T[] toArray(T[] a) {
        if (a.length < size){
            // Make a new array of a's runtime type, but my contents:
            @SuppressWarning("unchecked")
            T[] result = (T[])Arrays.copyOf(elements,size,a.getClass());
            return result;
        }
        System.arraycopy(elementData, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }
上一篇下一篇

猜你喜欢

热点阅读