簡體   English   中英

我應該使用@SuppressWarnings - 類型安全:從Object []到T []的未選中強制轉換

[英]Should I use @SuppressWarnings - Type safety: Unchecked cast from Object[] to T[]

試圖制作一個可增長的數組,即一個容量可以像arraylist那樣增加的數組。 我在下面的代碼中收到警告。 我應該修理它還是抑制它? 抑制它有什么后果?

import java.util.*;

public class GrowableArray<T>{
  private T[] array;
  //more variables

GrowableArray{
  this.array = (T[]) new Object[10]; // Warning - Type safety: Unchecked cast 
                                     //from Object[] to T[]
  //more code

 }     

//more code

完整代碼看下面 -

import java.util.*;  

public class GrowableArray<T>{  

    private T[] array;  
    private int increaseSizeBy;  
    private int currentIndex;//That is first free position available  
    private int lastIndex;  

    public GrowableArray(){       
        this.array = (T[]) new Object[10];  
        this.currentIndex = 0;  
        this.lastIndex = 10-1;  
        this.increaseSizeBy = 10;  
    }  


    public GrowableArray(int initialSize){  
        this.array = (T[]) new Object[initialSize];  
        currentIndex = 0;  
        lastIndex = initialSize - 1;  

    }  

    public void increaseSizeBy(int size){  
        this.increaseSizeBy = size;  

    }  


    public void add(T anObject){  

        if(currentIndex > lastIndex){ ;  
            //create a bigger array  
            int oldLength = array.length;  
            int newLength = oldLength + this.increaseSizeBy;  
            Object [] biggerArray = Arrays.copyOf(array, newLength);  
            array = (T[]) biggerArray;  
            currentIndex = oldLength;  
            lastIndex = array.length-1;  

        }else{  
            array[currentIndex] = anObject;  
            currentIndex++;  

        }  

    }  


    public void display(){  

        System.out.println();  

        for(int i = 0; i < this.currentIndex; i++){  
            System.out.print(array[i] + ", ");  

        }  

        System.out.println();  

    }  


    public static void main(String[]args){  

        GrowableArray<Integer> gArr = new GrowableArray<Integer>();  

        for(int i = 0; i <= 35; i++){  
            gArr.add(i);  

        }  

        gArr.display();  

        gArr.add(300);  
        gArr.add(301);  
        gArr.add(302);  
        gArr.add(303);  
        gArr.add(304);  
        gArr.add(305);  


        gArr.display();  

    }  


}  

在Java 7中,您可以使用varargs。 根本沒有壓制:

public class GrowableArray<T> {
  // Default empty to size 10.
  private static final int DefaultLength = 10;
  // My current array.
  private T[] array;

  // Empty constructor.
  GrowableArray () {
    // Passing no 2nd param at all forces jvm to manufacture an empty one for me - which is an array<T>.
    array = makeNew(DefaultLength);
  }

  // Make a new one of the right size.
  private T[] makeNew(int length, T... sample) {
    return Arrays.copyOf(sample, length);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM