gpt4 book ai didi

java - 类型安全 : The expression of type Map[] needs unchecked conversion to conform to Map[]

转载 作者:行者123 更新时间:2023-12-01 16:44:48 27 4
gpt4 key购买 nike

我有以下两行:

Map<String,Object>[] IEXDivMap = null;       

IEXDivMap = new Map[IEXJsonArray.length()];

并收到警告:

The expression of type Map[] needs unchecked conversion to conform to Map<String,Object>[]

有办法解决吗?

更新:

有人在评论中问我为什么我们首先需要一个 Map 数组。我们正在获取一系列 HashMap 并将每个 HashMap 放置在 map 数组中。代码如下:

@SuppressWarnings("unchecked")
public static Map<String,Object>[] getDiv(String ticker) {
Map<String,Object>[] IEXDivMap = null;
try{
String url = "https://api.IEXtrading.com/1.0/stock/" + ticker + "/dividends/1y";
URL obj = new URL(url);

HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
if(responseCode == 404){
System.out.println("Ticker " + ticker + " NOT FOUND in getDiv()!");
return IEXDivMap;
}else if (responseCode != 200){
System.out.println("IEX Printing All Response Header for URL: " + obj.toString() + "\n");
Map<String, List<String>> map = con.getHeaderFields();
for(Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("IEX " + entry.getKey() + " : " + entry.getValue());
}
}

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();

JSONArray IEXJsonArray = new JSONArray(response.toString());
IEXDivMap = new Map[IEXJsonArray.length()];

for (int i = 0; i < IEXJsonArray.length(); i++) {
IEXDivMap[i] = new HashMap<String,Object>();
JSONObject IEXJsonObject = IEXJsonArray.getJSONObject(i);

IEXDivMap[i].put("exDate",IEXJsonObject.getString("exDate"));
IEXDivMap[i].put("amount",IEXJsonObject.getString("amount"));

//System.out.println(IEXDivMap[i]);
System.out.println(IEXDivMap[i].get("exDate") + " 0 " + IEXDivMap[i].get("amount"));
}
}catch(Exception e){
System.out.println("FATAL ERROR: Something went wrong in getDiv " + e.getMessage());
System.exit(0);
}
return IEXDivMap;
}

最佳答案

不幸的是,不可能以干净的方式解决这个问题。

干净的解决方案是创建通用数组,但由于类型删除,这是不可能的。

为什么禁止创建泛型数组?

考虑以下数组示例:

Object[] arr = new String[1];
arr[0] = 10;

结果是ArrayStoreException

现在假设允许创建通用数组:

Map<String, String>[] map = new Map<>[1]; // This is illegal
Object[] objects = map;
objects[0] = new HashMap<Integer, Integer>(); // No ArrayStoreException

编译器警告人们可以将任何Map放入数组中,并且编译器和运行时都无法检查它。因此发出警告

解决方法?

引用Java generics faq你可以使用:

  • array of raw type
  • array of unbounded wildcard parameterized type
  • collection instead of array
<小时/>

我个人强烈建议您考虑使用列表:

List<Map<String, Object>> list = new ArrayList<>();

关于java - 类型安全 : The expression of type Map[] needs unchecked conversion to conform to Map<String, 对象>[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53973126/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com