문제

Im trying to populate a map that has been declared as an instance variable but nothing happens when i execute the code.

Here is my class (very basic/early)

public class RacingDrivers
{
private Map<String, String> racingNumbers;

/**
* Constructor for objects of class RacingDrivers
*/
public RacingDrivers()
{
   racingNumbers = new HashMap<String, String>();
}

/**
* A method to populate the racingNumbers map.
* 
* 
*/
public void populateMap()
{
  racingNumbers.put("44", "Lewis Hamilton");
  etc etc

}

Then i execute this code:

RacingClub RC = new RacingClub();
RC.populateMap();

RC is created but the map is not populated?

도움이 되었습니까?

해결책

You're creating an instance of class RacingClub.

The racingNumbers Map is an instance field of class RacingDrivers.

The Map is initialized in the constructor of the RacingDrivers class.

다른 팁

you initialize the class RacingDrivers

and then call

RacingDrivers  rc = new RacingDrivers();

rc.populateMap();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top