Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

i'm new to Java. How can i store an array of integers values in a HashMap, after that i write this HashMap in a txt file but this isn't important at the moment. I can store single fields but not an array. Any ideas ?

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");     
    }   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
101 views
Welcome To Ask or Share your Answers For Others

1 Answer

HashMap<String, List<Integer>> map = new HashMap<String, List<Integer>>();
HashMap<String, int[]> map = new HashMap<String, int[]>();

pick one, for example

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]); 
}

or just

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...