2014-10-23 3 views
0

에 모든 값을 추가 할 수 없습니다 :지도 아래 for 루프에서

for(int i = 0; i < initialSplit.length; i++){ //initialSplit - contains one DB record (row) 
    String [] cellVal = (initialSplit[i]).split("~"); 
    System.out.println(cellVal[0]); 
    for(int j = 0; j < cellVal.length; j++){ //cellVal - contains value of each cell in a record 
     map.put(dataIndexString[j], cellVal[j]); 
     System.out.println(j+"--"+dataIndexString[j] + " -- key : value -- "+cellVal[j]); 
    } 
    items.add(map);//The last record alone adds into the ArrayList 'items' 
} 

initialSplit의 길이는 10 (10 개 기록)하고 cellVal.lengthHashMap의 추가에 21

입니다 ArrayList에 10 개의 맵 대신 마지막 맵이 10 번 추가됩니다. 각 맵은 한 번 추가됩니다. 나는 또한 map.clear(),하지만 어쩌면 잘못된 장소에 노력했다.
나는 그 실수를 발견 할 수 없다. 아무도 나를 도울 수 있습니까?

+0

왜 10 ** 다른 ** 맵을 추가 할 것이라고 생각 하는가? –

답변

4

목록에 여러 번 추가하는지도 인스턴스는 하나뿐입니다. 차이를하기 위해

목록에 추가 맵을, 당신이 만든 여러 인스턴스에 있습니다

map = new HashMap<...>(); 
    for(int j = 0; j < cellVal.length; j++){ //cellVal - contains value of each cell in a record 
     map.put(dataIndexString[j], cellVal[j]); 
     System.out.println(j+"--"+dataIndexString[j] + " -- key : value -- "+cellVal[j]); 
    } 
    items.add(map); 
+0

+1, 당신은 나의 날을 구했습니다. – Freakyuser