2016-02-27 83 views
0

我有一个方法动态返回一个动态返回一个ArrayList

ArrayList<Player> 

这里奋斗是我所:

幻想类

public class Fantasy { 
    Squad team; 
    ArrayList<Player> substitutes = team.getList(substitutes); 
} 

小队类

public class Squad { 
    ArrayList<Player> substitutes; 
    // Some code adding stuff into the ArrayList 

    public ArrayList<Player> getList(String list) { 
     return ArrayList<Player> list; << PROBLEM 
    } 
} 

我想有一个方法getList(),通过它我可以通过一个ArrayList,如果具有相同名称的ArratList存在,这将检查的名称,如果是,将其返回作为一个ArrayList。

问题是,我不知道如何检查是否有一个ArrayList与我传递的字符串list相同。

可能的解决方案:

Map<String, ArrayList<Player>> arrayLists = new HashMap<String, ArrayList<Player>>(){{ 
    put("goalKeepers", goalKeepers); 
    put("defenders", defenders); 
    put("midFielders", midFielders); 
    put("strikers", strikers); 
    put("substitutes", substitutes); 
}}; 

public ArrayList<Player> getList(String list) { 
    return arrayLists.get(list); 
} 

但是,当我打电话:

ArrayList test = getList("substitute"); 

或每当我使用的GetList();我得到以下错误:

Cannot make a static reference to the non-static method getList(String) from the type Squad

+0

我不完全理解你的问题,但是让'getSubstitutes()'方法返回列表:'return substitutes;'是否更有意义? – Vulcan

+0

“Squad”类中只有一个List,因此只有一个可能的列表返回。此外,你的陈述“检查这种方法是否存在”与你的其他问题没有任何关系。也许你应该在Java应用程序的上下文中查找术语“方法”的含义。一般而言,您的问题与目前所述无关。 –

+2

列表从哪里来?你是否试图通过名称将它从任何对象成员中拉出来?这很少是一个好主意,为什么不使用'Map >'如果你需要将字符串配对玩家集合 –

回答

1

不是存储替代品的名单中不同的实例变量,你应该有一个存储团队的名称映射到玩家对应的列表中选择一个领域:

public class Squad { 

    Map<String, List<Player>> substitutes = new HashMap<>(); 

    // add the player lists into the map 

    /* 
    * Returns the list of players for the given team name, 
    * can be {@code null} if no player list has been stored 
    * with the team name. 
    */ 
    public List<Player> getList(String teamName) { 
     return substitutes.get(teamName); 
    } 
} 
+0

我已经根据你的解决方案和我得到的错误更新了我的帖子。但它似乎应该起作用。 –

+0

看来你没有按照我的建议去做,而是使用[double brace initialization]声明'arrayLists'变量(http://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-在-JAVA)。只需添加'substitutes.put()'调用,例如在“Squad”的构造函数中;它会工作。 –

0

有没有办法在使用分配给在逻辑或代码的流变量/方法的名称Java语言实现。

你将不得不自己实现它,例如:

public ArrayList getList(String name) { 
    if(name.equals("substitutes") && substitutes != null) { 
     return substitutes; 
    } 

    else { 
     //handle error as you like 
    } 
} 

而且你可以添加你要访问其他列表或对象else if语句。

+0

或者有一个'Map ' – Stewart

0

如果你的球队保持

HashMap<String, ArrayList<Player>> 

,你可以用它来返回给一个字符串键ArrayList中。

0

你可以使用

Map<String,ArrayList<>> myMap = new HashMap<String,ArrayList<>>(); 

在这里你可以找到哪些arraylist字符串键可用。

0

想要做的事可以通过使用反射来实现。实施将是这个样子:

public List<Player> getList(String list) { 
    try { 
     Field field = this.getClass().getDeclaredField(list); 
     return (List<Player>) field.get(this); 
    } catch (IllegalAccessException e) { 
     // do something sensible 
    } 
} 

这将检查当前对象与您所提供的名称成员字段,并返回该字段的值。一般来说,你应该避免反思,直到你真的知道你在做什么,没有别的选择。

就你而言,你似乎遇到了Java是一种编译语言的问题,并没有提供一些可用于通用脚本语言的功能。编译语言的一个主要特点是可以依赖语言来为您找到参考。使用反射尝试将字符串匹配到字段,可以抛弃此优点。 因此,更好的解决方案可能会像其他答案的建议一样使用Map。

最后一件事:遵循OOP的SOLID原则,您应该依赖List接口而不是类型声明中的ArrayList实现。 (SOLID中的'D')