2016-10-04 5 views
0

그래서 Star Trek 테마 프로젝트를 수행하여 내가 방문한 승무원과 행성의 목록을 가져 와서 로그 보고서를 생성해야합니다. 스타 플릿. 나는를 사용하기로 결정 그래서 예를 들어이 들어 트리 맵에서 int 배열의 요소에 액세스하고 수정하는 방법

...

Guinan,Drema IV 
Picard,Gamalon V 
Barclay,Valo III 
Riker,Theydat IV 
Pulaski,Alpha Moon 
Troi,Tessen III 
... 

요구 내가 자동으로 들어오는 데이터를 정렬하는 일반적인 구조를 사용하기 위해 필요한이 작업을 수행하려면이

Acamar III: 
    B. Crusher    11 
    Barclay     6 
    Data     15 
    Gomez     3 
    Guinan     4 
    Lefler     5 
    O'Brien    12 
    Ogawa     4 
    Picard     5 
    Pulaski    14 
    Riker     12 
    Troi     9 
    W. Crusher    4 
    Worf     14 
    Yar      3 
... 

될 수 있습니다 Tree Map 15 요소 int 배열을 사용하여 각 승무원이 주어진 행성에 방문한 횟수를 저장합니다. 나는 자바 매우 새로운 오전부터

내 질문은 어떻게 배열 트리 맵 내부 요소와 갱신 주어진 배열 요소 내부의 값에서 값를 얻을 수 것입니까? 내 문제는 Tree Map을 사용하여 작업 할 때 찾을 수있는 모든 예제 중 하나가 내부의 배열 또는 don't show how to get a value and update values after the first insertion을 포함하지 않는다는 것입니다. 아래에서 나는 이것을 수행하는 방법에 관해서 나의 가장 좋은 추측으로 현재의 의사 코드를 주었다. 누구든지이 일을 할 수있는 방법을 알고 있거나 더 좋은 방법을 전적으로 제안하십시오.

P. 필요한 루프를 구현할 것입니다 다음에 올바르게 작성된 단일 반복을 얻을 수 있습니다.

EDIT : 명확하게하기 위해 15 요소 int 배열의 각 요소는 승무원에 해당하므로 예를 들어 데이터가됩니다. 당신이 뭔가를 할 수 array[2]와 야르가 될 것 array[14]

import java.util.*; 
public class TreeMapDemo { 

    public static void main(String args[]) { 
     // Create a hash map 
     TreeMap tm = new TreeMap(); 
     int indexDesired; 
     int visits; 
     String planetNameVariable; 
     String crewMemberName; 

     //Scan input using Scanner and assign planet name and crew name to 
     //correct variables (code provided by instructor) 

     // Put elements to the map 
     //if(planet doesn't already exist in tm) 
      tm.put(planetNameVariable, new int[14]); 

     //Decides which element of the array must be incremented 
     indexDesired = crewToIndex(crewMemberName); 

     //Increments visit count of crewMemberName on planetNameVariable 
     visits = //How do I get the value of the array associated with planetNameVariable at indexDesired? 
     tm.put(planetNameVariable, int[indexDesired] = visits + 1 //How do I insert into an array element here?); 

     // Get an iterator 
     Iterator i = set.iterator(); 

     // Display element 
     // Code not designed yet 

    } 
} 

답변

1

. 여기서는 배열을 한 번만 넣어야합니다. 이후에는 참조 만 얻을 수 있으므로 수정하면지도에서도 수정됩니다. [얕은 사본]