将数组存储在HashMap中
我是Java新手。我如何在HashMap中存储整数值数组,之后我将此HashMap写到txt文件中,但此刻目前不重要。我可以存储单个字段,但不能存储数组。有任何想法吗
?
public void salveazaObiectulCreat(String caleSpreFisier) { HashMap map = new HashMap();
map.put ("Autorul",numelePrenumeleAutorului);
map.put ("Denumirea cartii",denumireaCartii);
map.put ("Culoarea cartii",culoareaCartii);
map.put ("Genul cartii",gen);
map.put ("Limba",limba);
map.put ("Numarul de copii",numarulDeCopii);
map.put ("Numarul de pagini",numarulDePagini);
map.put ("Pretul cartii",pretulCartii);
try {
File file = new File(caleSpreFisier);
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(map);
s.close();
} catch(Exception e){
System.out.println("An exception has occured");
}
}
回答:
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();HashMap<String, int[]> map = new HashMap<String, int[]>();
例如选一个
HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();map.put("Something", new ArrayList<Integer>());
for (int i=0;i<numarulDeCopii; i++) {
map.get("Something").add(coeficientUzura[i]);
}
要不就
HashMap<String, int[]> map = new HashMap<String, int[]>();map.put("Something", coeficientUzura);
以上是 将数组存储在HashMap中 的全部内容, 来源链接: utcz.com/qa/425915.html