I don´t see where my bug is here, what I got is an array of Strings containing the following values, line by line:
San Jose -> San Francisco
San Jose -> Anchorage
New York -> Anchorage
New York -> San Jose
New York -> San Francisco
New York -> Honolulu
Anchorage -> New York
Anchorage -> San Jose
Honolulu -> New York
Honolulu -> San Francisco
Denver -> San Jose
San Francisco -> New York
San Francisco -> Honolulu
San Francisco -> Denver
I want to put these values in a HashMap using the string on the right as the key, and have the value be an ArrayList, so that if I ask for destinations for 'San Jose', it will iterate through the list and return 'San Francisco' and 'Anchorage'. I haven´t managed to get the Key-Value relationship working correctly. Cause when the method is done running. For any city the value will be that of it´s last iteration, (San Francisco) returning New York, Honolulu and Denver for all keys.
private void createDataBase()
{
ArrayList<String> tempValues = new ArrayList <String>();
for (int i = 0; i < database.size();i++)
{
String line = database.get(i);
//String value = "";
if (line.length() > 1)
{
int keyStart = 0;
int keyEnd = line.indexOf("-");
int valueStart = keyEnd+3;
int valueEnd = line.length();
String key = line.substring(keyStart, keyEnd);
String value = line.substring(valueStart, valueEnd);
tempValues.add(value);
System.out.println(keyValueDatabase.toString());
keyValueDatabase.put(key, tempValues);
}
else
{
tempValues.clear();
}
}
System.out.println(keyValueDatabase.toString());
}