【问题标题】:How do I retrieve particular value from an ArrayList<String> in Spring如何在 Spring 中从 ArrayList<String> 中检索特定值
【发布时间】:2020-08-05 14:45:25
【问题描述】:

客户端程序应显示所有玩家的详细信息及其对应的国家/地区详细信息。如果给定一个国家名称,它还应该显示属于该国家的所有玩家名称。两名球员属于一个国家,另外 3 名球员属于另一个国家。

我在 Spring 的 beans.xml 文件中声明了 4 个列表。我想要 知道如何检索特定的列表值吗?

我的 beans.xml 文件:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<bean id="player" class="com.my.bean.SpringIOC5">
    <property name="playerId">
    <list>
    <value>p1</value>
    <value>p2</value>
    <value>p3</value>
    <value>p4</value>
    <value>p5</value>
    </list>
    </property>
    <property name="playerName">
    <list >
    <value>pn1</value>
    <value>pn2</value>
    <value>pn3</value>
    <value>pn4</value>
    <value>pn5</value>
    </list>
    </property>
    <property name="country" ref="cnty"></property>
    </bean>
    
    <bean id="cnty" class="com.my.bean.Country">
    <property name="countryId">
    <list>
    <value>c1</value>
    <value>c2</value>
    <value>c3</value>
    <value>c4</value>
    <value>c5</value>
    </list>
    </property>
    <property name="countryName">
    <list>
    <value>cn1</value>
    <value>cn2</value>
    <value>cn3</value>
    <value>cn4</value>
    <value>cn5</value>
    </list>
    </property>
    </bean>
    </beans>

我的播放器类:

    public class SpringIOC5 {
    //public class Player
private List<String> playerId;
private List<String> playerName;
private Country country;
public SpringIOC5() {
    super();
    // TODO Auto-generated constructor stub
}
public SpringIOC5(List<String> playerId, List<String> playerName, Country country) {
    super();
    this.playerId = playerId;
    this.playerName = playerName;
    this.country = country;
}
public List<String> getPlayerId() {
    return playerId;
}
public void setPlayerId(List<String> playerId) {
    this.playerId = playerId;
}
public List<String> getPlayerName() {
    return playerName;
}
public void setPlayerName(List<String> playerName) {
    this.playerName = playerName;
}
public Country getCountry() {
    return country;
}
public void setCountry(Country country) {
    this.country = country;
}
@Override
public String toString() {
    return "SpringIOC5 [playerId=" + playerId + ", playerName=" + playerName + ", country=" + country + "]";
}

}

我的国家班级:

    public class Country {
private List<String> countryId;
private List<String> countryName;
public Country() {
    super();
    // TODO Auto-generated constructor stub
}
public Country(List<String> countryId, List<String> countryName) {
    super();
    this.countryId = countryId;
    this.countryName = countryName;
}
public List<String> getCountryId() {
    return countryId;
}
public void setCountryId(List<String> countryId) {
    this.countryId = countryId;
}
public List<String> getCountryName() {
    return countryName;
}
public void setCountryName(List<String> countryName) {
    this.countryName = countryName;
}
@Override
public String toString() {
    return "Country [countryId=" + countryId + ", countryName=" + countryName + "]";
}

}

【问题讨论】:

    标签: java spring list dependency-injection inversion-of-control


    【解决方案1】:

    使用 ArrayList 类的方法获取具体的值。

    countryId.get(indexNo.); 
    

    在此处查看 oracle java 文档中的更多方法

    https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 1970-01-01
      • 2015-05-20
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2021-06-30
      相关资源
      最近更新 更多