如何为bean设置循环数组值

时间:2017-12-08 09:34:04

标签: java spring

我在控制器类中有以下循环:

for (int i = 0; i <= locationArr.length - 1; i++) {
    data.put(idArr[i], locationArr[i]);
    locationBean.setLocation_name(locationArr[i]);
}

My Bean看起来像:

 public class LocationBean {
     private String region_id;
     private String region_name;
     private String location_id;
     private String location_name;

     //getters and setters  
 }

我正在尝试将location_name设置为setLocation_name(locationArr [i]); 但是只获得了循环[i]的最后一个值。

3 个答案:

答案 0 :(得分:0)

如果您想拥有多个位置名称,可以通过以下方式进行操作:如果订单无关紧要,则列表或更一般的任何收集。

这是一个例子:

public class LocationBean {
   private String region_id;
   private String region_name;
   private String location_id;
   private List<String> locationNames = new ArrayList<>();

   //getters and setters 
   public List<String> getLocationNames() {
        return locationNames;
   } 
}

循环中的用法:

locationBean.getLocationNames().add(locationArr[i]);

我没有重构所有示例代码以符合java命名约定。你应该用camel case命名你的变量。

答案 1 :(得分:0)

带有List 的

LocationBean

您希望存储每个位置名称,而不是设置一个值。因此,您希望方法addLocationName将其存储到Collection

locationBean.addLocationName(locationArr[i]);

该方法很简单,它会将每个String添加到List<String>

private List<String> locationsName;
private List<String> locationsId;


public LocationBean (){
    locationsName = new ArrayList<String>();
    locationsId= new ArrayList<String>();
}

public boolean addLocationName(string locationName){
    return this.locationsName.add(locationName);
}

public boolean addLocationId(string locationId){
    return this.locationsId.add(locationId);
}

当然,您需要对location_id执行相同操作,因此Bean会更聪明:

public class Location{
    private String id;
    private String name;

    public Location(String id, String name){ ... }
    //constructor and getter
}

只需使用List<Location>即可。这样,idname都会存储在一起。

public boolean addLocation(Location location){
    return this.locations.add(location);
}

或传递值

public boolean addLocation(String id, String name){
    return this.locations.add(new Location(id, name));
}

LocationBean列表

或者你的bean应该只有一个位置然后这就在你的循环中你需要将LocationBean的每个实例存储到List<LocationBean>中(不要忘记每次创建一个新实例) `

List<LocationBean> locations = new ArrayList<>();
for (int i = 0; i <= locationArr.length - 1; i++) {
    locationBean = new LocationBean(); //new instance each time

    locationBean.setLocation_name(locationArr[i]);

    locations.add(locationBean); //add into the list
}

答案 2 :(得分:0)

你可以创建一个LocationBean对象列表:

ArrayList <LocationBean> locationBeanList = new ArrayList <LocationBean>)();

for (int i = 0; i <= locationArr.length - 1; i++) {
        data.put(idArr[i], locationArr[i]);
        locationBean = new LocationBean();
        locationBean.setLocation_name(locationArr[i]);
        locationBeanList.add(locationBean);

    }

或者,您可以在单个位置bean中创建位置列表

 public class LocationBean {
 private String region_id;
 private String region_name;
 private String location_id;
 private List<String> location_name_list = new ArrayList<String>();

 //getters and setters  
 }

List<String> locationList = new ArrayList <String>();
 for (int i = 0; i <= locationArr.length - 1; i++) {
        data.put(idArr[i], locationArr[i]);

        locationList.add(locationArr[i]);

    }

locationBean.setLocation_name_list(locationList );