美文网首页
第 27 条:消除非受检警告

第 27 条:消除非受检警告

作者: 综合楼 | 来源:发表于2021-05-10 19:50 被阅读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;
        }
    

    相关文章

      网友评论

          本文标题:第 27 条:消除非受检警告

          本文链接:https://www.haomeiwen.com/subject/cxxjdltx.html