java – 将数组存储在HashMap中

前端之家收集整理的这篇文章主要介绍了java – 将数组存储在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.put("Something",new ArrayList<Integer>());
for (int i=0;i<numarulDeCopii; i++) {
    map.get("Something").add(coeficientUzura[i]); 
}

要不就

HashMap<String,int[]>();
map.put("Something",coeficientUzura);

猜你在找的Java相关文章