2012-10-24 42 views
3

我有這個方法():ArrayList是一種原始類型。引用泛型類型的ArrayList <E>應該是參數

private List<? extends Map<String, String>> creaListaDeGrupos() { 

     ArrayList resultado_padre = new ArrayList(); 
     for (int i = 0; i < listadoPartidas.getPartidas().size(); i++) { 
      HashMap<String, String> padre = new HashMap<String, String>(); 
      padre.put("posicionLista", posicionlista.get(i)); 
      padre.put("codigo", codigoBaremo.get(i)); 
      padre.put("descripcionBaremo", descripcionBaremoPadre.get(i)); 
      padre.put("cantidad", cantidad.get(i)); 
      padre.put("importe", importe.get(i)); 
      padre.put("estado", estado.get(i)); 
      padre.put("observaciones", observaciones.get(i)); 
        resultado_padre.add(padre); 
     } 
return resultado_padre 
} 

和絨毛返回我的錯誤:

ArrayList is a raw type. References to generic type ArrayList should be parameterized

,但我不能做

ArrayList<String> resultado_padre = new ArrayList(); 

因爲這不是字符串的數組列表,什麼類型的半身像會是?

回答

0
private List<Map<String, String>> creaListaDeGrupos() { 

      List<Map<String, String>> resultado_padre = new ArrayList<Map<String, String>>() ; 
      for (int i = 0; i < listadoPartidas.getPartidas().size(); i++) { 
       Map<String, String> padre = new HashMap<String, String>(); 
       padre.put("posicionLista", posicionlista.get(i)); 
       padre.put("codigo", codigoBaremo.get(i)); 
       padre.put("descripcionBaremo", descripcionBaremoPadre.get(i)); 
       padre.put("cantidad", cantidad.get(i)); 
       padre.put("importe", importe.get(i)); 
       padre.put("estado", estado.get(i)); 
       padre.put("observaciones", observaciones.get(i)); 
       resultado_padre.add(padre); 
      } 
return resultado_padre 
} 
+0

類型不匹配:不能從轉換的ArrayList <地圖>到的ArrayList > – colymore

+0

對不起。這應該工作。 – Harry

1

你可以嘗試創建您返回相同的類型:

List<HashMap<String, String>> = new ArrayList<HashMap<String, String>>(); 

沒有必要宣佈實現類型,即ArrayListList接口更通用,因此在將變量聲明爲具體類型時,問問自己是否有必要。見 Programming against interface

+0

是的!它的工作原理,謝謝 – colymore

+0

@colymore歡迎:) – kostja

+0

同樣在DRY的精神中,您不需要在等號的右側複製特定的泛型:List > = new的ArrayList <>(); – geowar

0

這將是ArrayList<HashMap<String, String>>

相關問題