这是我到目前为止所拥有的,我不知道如何访问拆分的两个部分,这段代码确实是错误的,但我不知道如何做我想做的事(是的,它是为了学校)
public class Relatives
{
private Map<String,Set<String>> map;
/**
* Constructs a relatives object with an empty map
*/
public Relatives()
{
map = new TreeMap<String,Set<String>>();
}
/**
* adds a relationship to the map by either adding a relative to the
* set of an existing key, or creating a new key within the map
* @param line a string containing the key person and their relative
*/
public void setPersonRelative(String line)
{
String[] personRelative = line.split(" ");
if(map.containsKey(personRelative[0]))
{
map.put(personRelative[0],map.get(personRelative[1])+personRelative[1]);
}
else
{
map.put(personRelative[0],personRelative[1]);
}
}
我试图访问该人并添加到那里当前的亲戚,如果不存在,则与该亲戚创建一个新人
我将如何格式化它使它像这样返回
Dot is related to Chuck Fred Jason Tom
Elton is related to Linh
我有这个但得到错误
public String getRelatives(String person)
{
return map.keySet();
}