2016-11-21 3 views
2

자바에서 텍스트 기반 게임을하고 있으며지도 용 사전과 같은 python을 사용하고 싶습니다. HashMap은 실제로 하나의 사전에 연결된 여러 값을 갖고 있기 때문에 실제로 작동하지 않습니다.파이썬에서와 비슷한 자바 사전을 만드는 방법은 무엇입니까?

room_x = { "name": "X", 

"description":"Random description", 

"exits": {linked to another dictionary}, 

"items": list_of_players_items} 

자바에서 가능한 비슷한인가 : 파이썬에서

이 같은 사전을 정의 할 수 있습니다?

미리 감사드립니다.

+0

객체 지향 언어를 사용하여 좋은 작은 객체 지향 프로그래머와 같은 당신이 필드 클래스를 생성하지 않는 이유라도? – Andreas

+0

새 질문을하기 전에 몇 가지 Google 검색을 시도해보십시오. 특히 일반적인 작업에 대해서는 특히 그렇습니다. – TigerhawkT3

+1

@ TigerhawkT3 참고로 HashMap 솔루션을 특별히 요구하지는 않았지만 Python에서 java (HashMap보다 나은 솔루션을 원한다면)에서 사전 함수를 복제하는 방법을 묻습니다. HashMap 솔루션이이를 수행하는 최선의 방법으로 밝혀 졌다고해서 태그를 추가 한 것과 같은 문제가되지는 않습니다. – MarkwinVI

답변

3

HashMap는 작업을 수행 할 수 있습니다

HashMap<String, String> hashMap = new HashMap<>(); //<key,value> both key and value are Strings 
hashMap.put("name", "X"); 
hashMap.put("description", "Random description"); 

여러 값을 링크. 당신이 코드 위

HashMap<String,HashMap<String, String>> linkedMap = new HashMap<>(); // key is string and value is HashMap<String, String> 
HashMap<String,String> itemsMap = new HashMap<>(); 
itemsMap.put("item1", "this is first item"); 
itemsMap.put("item2", "this is second item"); 
linkedMap.put("items", itemsMap); 

으로 키 값 쌍 형식을 변경해야하는 것은 내가 쓴 그냥 빨리 예를 들어, 당신은 데이터의 실제 타입을 선언 할 필요가 String 필요는 없습니다.
희망이 있습니다.

0

HashMap 또는 Hashtable이면 충분합니다. 둘 다 키 - 값 쌍을 가지며, 여기서 key는 String이고 value는 모든 객체가 될 수 있습니다. 그래서 이것을 HashMap<String, Object> map = new HashMap<String, Object>()과 같이 정의 할 수 있습니다.

값은 무엇이든 가능합니다 (HashMap은 HashTable이 아니더라도 null도 가능).

{다른 사전에 링크} 그리고 "항목"해결 값으로 List : list_of_players_items

그래서 당신은 "종료"를 해결 값과 같은 다른 HashMap을 가질 수 있습니다

This은 HashMap과 Hashtable의 차이점을 나타냅니다.

샘플 코드 :

List players = // some List 
HashMap exits = // some Map 
HashMap<String, Object> room_x = new HashMap<String, Object>(); 
map.put("name",name_string); 
map.put("desc",desc_string); 
mp.put("exits",exits); 
map.put("items",players); 
관련 문제