2011-09-30 4 views
2

어떻게해야합니까? HashMap의에서 내가 저장 내 필드, 나는 간단한 객체HashMap에서 배열로 가져온 ArrayList를 변환

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

    ArrayList arl=new ArrayList(); 

    for (int i = 0;i<numarulDeCopii;i++) { 

    arl.add(coeficientUzura[i]); 
} 
    map.put ("Coeficientii de Uzura",arl); 

내가 (마지막 라인을 찾아)의 HashMap에서 값을 액세스의

Carte (String caleSpreFisier) { 

HashMap map = new HashMap(); 

File file = new File(caleSpreFisier); 

try { 

FileInputStream f = new FileInputStream(file); 
ObjectInputStream s = new ObjectInputStream(f); 
map = (HashMap)s.readObject();   
s.close(); 

} catch(Exception e){ 

      System.out.println("An exception has occured : "+e);  
    } 

for (Object key :map.keySet()) { 

    if (key.equals("Autorul")) { 

     numelePrenumeleAutorului = (String)map.get(key); 

    } 

     if (key.equals("Denumirea cartii")) { 

     denumireaCartii = (String) map.get(key); 
    } 

    if (key.equals("Culoarea cartii")) { 

     culoareaCartii = (String)map.get(key); 
    } 


    if (key.equals("Genul cartii")) { 

     gen = (String) map.get(key); 

    } 

    if (key.equals("Limba")) { 

     limba = (String) map.get(key); 

    } 

    if (key.equals("Numarul de copii")) { 

     numarulDeCopii = (Integer) map.get(key); 

    } 

    if (key.equals("Numarul de pagini")) { 

     numarulDePagini = (Integer) map.get(key); 

    } 

    if (key.equals("Pretul cartii")) { 

     pretulCartii = (Double) map.get(key); 
    } 

    if (key.equals("Coeficientii de Uzura")) { 

     ArrayList temp = new ArrayList(); 


     Object me = map.get(key); 

     System.out.println(me); 

     //temp = (ArrayList) map.get(key); 
    } 
} 

답변

0
Object[] array = ((ArrayList) map.get(key)).toArray(); 
0

처음처럼을했다 : 당신은 object denial. 귀하의 HashMap은 실제로 귀하가 다른 필드를 보유하기 위해 실제 속성으로 작성한 적절한 클래스 여야합니다.

그런 식으로 그 "못된"속성 이름 "- 로컬 변수 매핑을 수행 할 필요가 없습니다.

다음 : 당신이지도에 넣어 것은 ArrayList입니다 경우, 당신은 단지 당신이 다시 형에게 무엇을 얻을 캐스트 할 수 있습니다 : 속성의 이름에 대한

ArrayList temp = (ArrayList) map.get(key); 
+0

들으,하지만이입니다 제 모국어로 타이핑 된 이름이 나옵니다. –