2017-10-11 19 views
-3
public class Main { 

     public static void main(String[] args) { 

      Integer[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
      print_generic(arr); 
     } 

     public static <T> void print_generic(T arr) { 
//  for (int i = 0; i < arr.length; i++) { //ERROR 
//   System.out.print(arr[i] + " "); 
//  } 
      System.out.println(); 
      System.out.print(arr); 
     } 

    } 

錯誤:無法解析符號長度一個數組中不能使用arr.length通過在一個通用的方法

什麼問題
PLZ告訴我,我做錯了什麼

java visualizer image: even both the arr refers to the same object :

+1

'T'不一定是一個數組,所以就編譯器而言,沒有'length'屬性。 – Andreas

回答

4

參數的方法的必須是一個數組

public static <T> void print_generic(T[] arr) { } 
相關問題