Java获得编译警告



我正在编写一个它编译的程序,但代码警告我它使用了未检查或不安全的操作。使用-Xlint重新编译。我找不到错误。请帮我解决这个问题。

import java.io.*;
import java.util.*;
public class A1{
private HashMap<String,ArrayList<Integer>> data= new HashMap<String,ArrayList<Integer>>();
public  void main () throws IOException{
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(ob.readLine());
for(int i=0;i<t;i++){
String a=ob.readLine();
String spl[]= a.split(" ");
ArrayList<Integer> inputs= new ArrayList<Integer>();
for(int j=0;j<Integer.parseInt(spl[0]);j++){
int prices=Integer.parseInt(ob.readLine());
inputs.add(prices);
}
Collections.sort(inputs);
data.put(spl[1],inputs);
}
Iterator iter = data.entrySet().iterator();
while(iter.hasNext()){
Map.Entry ele = (Map.Entry)iter.next();
int fund=Integer.parseInt((String)ele.getKey());
System.out.println(maxhouse(fund,(ArrayList<Integer>)ele.getValue()));
}
}
int maxhouse(int fund,ArrayList<Integer> a){
int sum=0;
int c=0;
for(int i=0;i<a.size();i++){
sum=sum+a.get(i);
if(sum<fund){
c++;
}
else if(sum==fund){
c++;
break;
}
else{
break;
}
}
return c;
}
}

您可以在这里做一些更改,其中一些根据注释永远不应该使用RAW类型。因此,将迭代器更改为-

Iterator<Entry<String, ArrayList<Integer>>> iter = data.entrySet().iterator();

然后将您的Map.Entry更改为-

Entry<String, ArrayList<Integer>> ele = iter.next();

此外,您还可以利用java8Map.foreachlambda expressions来避免所有这些,并使代码变得更好、更易于呈现。

data.forEach((key,value) -> {
int fund = Integer.parseInt(key);
System.out.println(maxhouse(fund, value));
});

并避免编写更干净的代码。

Iterator iter = data.entrySet().iterator();
while(iter.hasNext()){
Map.Entry ele = (Map.Entry)iter.next();
int fund=Integer.parseInt((String)ele.getKey());
System.out.println(maxhouse(fund,(ArrayList<Integer>)ele.getValue()));
}

最新更新