Java 修改ArrayList中的值

Java 修改ArrayList中的值,java,list,arraylist,Java,List,Arraylist,我有以下代码: List<Banner> bannerLists = ...; // Get the list for (Banner bannerList : bannerLists) { bannerList.getLocation().setLocationName("1 - " + bannerList.getLocation().getLocationName()); log.info("LOCATION " +

我有以下代码:

List<Banner> bannerLists = ...; // Get the list
for (Banner bannerList : bannerLists) {
    bannerList.getLocation().setLocationName("1 - " 
                     + bannerList.getLocation().getLocationName());

    log.info("LOCATION " + bannerList.getId() + " : " 
                     + bannerList.getLocation().getLocationName());
}
Location.java

...
private String locationName;
...
怎么了?我尝试使用
for(int I=0;I…
,但效果相同

编辑

for (Banner bannerList : bannerLists) {
    Location location = banner.getLocation();
    location.setLocationName("1 - " + location.getPageName());
    bannerList.setLocation(location);

    log.info("LOCATION " + bannerList.getId() + " : " 
                     + bannerList.getLocation().getLocationName());
}

当您得到
locationName
时,您试图递归地设置它。我假设您想要它的位置。您可以将引用
Location
保存到一个变量中。总之,类似

List<Banner> bannerLists = ...; // Get the list
int pos = 0;
for (Banner banner : bannerLists) {
  Location location = banner.getLocation();
  String locName = Integer.toString(pos++);
  location.setLocationName(locName);
  log.info("LOCATION " + bannerList.getId() + " : " + locName);
}
List banner列表=…;//获取列表
int pos=0;
用于(横幅:横幅列表){
位置=banner.getLocation();
字符串locName=Integer.toString(pos++);
location.setLocationName(locName);
log.info(“位置”+bannerList.getId()+”:“+locName”);
}

List banner列表=…;//获取列表
对于(int pos=0,len=bannerLists.size();pos
当您得到它时,您正试图递归设置
locationName
。我假设您想要它的位置。您可以将引用
位置
保存到一个变量。总之,类似

List<Banner> bannerLists = ...; // Get the list
int pos = 0;
for (Banner banner : bannerLists) {
  Location location = banner.getLocation();
  String locName = Integer.toString(pos++);
  location.setLocationName(locName);
  log.info("LOCATION " + bannerList.getId() + " : " + locName);
}
List banner列表=…;//获取列表
int pos=0;
用于(横幅:横幅列表){
位置=banner.getLocation();
字符串locName=Integer.toString(pos++);
location.setLocationName(locName);
log.info(“位置”+bannerList.getId()+”:“+locName”);
}

List banner列表=…;//获取列表
对于(int pos=0,len=bannerLists.size();pos
在循环的每次迭代中都要更改位置名称,在开始处添加“1-”

bannerList.getLocation().setLocationName("1 - " 
                     + bannerList.getLocation().getLocationName());
因此,如果列表中的所有横幅共享同一位置,那么在每次迭代中,您将看到不同的名称


如果要为所有横幅打印相同的位置名称,请不要更新循环中的位置名称。

您正在循环的每个迭代中更改位置名称,并在开始处添加“1-”

bannerList.getLocation().setLocationName("1 - " 
                     + bannerList.getLocation().getLocationName());
因此,如果列表中的所有横幅共享同一位置,那么在每次迭代中,您将看到不同的名称


如果要为所有横幅打印相同的位置名称,请不要更新循环中的位置名称。

将第一个参数替换为变量,这样的情况会发生,因为您只需在循环中添加一个字符串和其他字符串

比如:

for i=0; i<3; i++
    print("1 + ")
for i=0; i<3; i++
        print(i + "some args")
修改为类似于:

for i=0; i<3; i++
    print("1 + ")
for i=0; i<3; i++
        print(i + "some args")

变量
计数器
将通过循环更新,并更新您的id

将第一个参数替换为变量,这种情况会发生,因为您只需通过循环添加一个字符串和其他字符串

比如:

for i=0; i<3; i++
    print("1 + ")
for i=0; i<3; i++
        print(i + "some args")
修改为类似于:

for i=0; i<3; i++
    print("1 + ")
for i=0; i<3; i++
        print(i + "some args")

变量
counter
将通过循环更新,并更新您的id

我相信这种行为的原因是所有横幅实际上都指向同一位置对象,而不仅仅是具有相同名称的对象

这将考虑到您已更新的任何位置:

ArrayList<Location> prev = new ArrayList<Location>();
for (Banner bannerList : bannerLists) {
    Location location = bannerList.getLocation();
    if (!prev.contains(location)) {
        location.setLocationName("1 - " + location.getLocationName());
        prev.add(location);
    }

    log.info("LOCATION " + bannerList.getId() + " : " 
                     + bannerList.getLocation().getLocationName());
}
ArrayList prev=new ArrayList();
用于(横幅列表:横幅列表){
Location Location=bannerList.getLocation();
如果(!prev.contains(位置)){
location.setLocationName(“1-”+location.getLocationName());
前添加(位置);
}
log.info(“位置”+bannerList.getId()+”:“
+bannerList.getLocation().getLocationName());
}

我认为这种行为的原因是所有横幅实际上都指向同一位置对象,而不仅仅是具有相同名称的对象

这将考虑到您已更新的任何位置:

ArrayList<Location> prev = new ArrayList<Location>();
for (Banner bannerList : bannerLists) {
    Location location = bannerList.getLocation();
    if (!prev.contains(location)) {
        location.setLocationName("1 - " + location.getLocationName());
        prev.add(location);
    }

    log.info("LOCATION " + bannerList.getId() + " : " 
                     + bannerList.getLocation().getLocationName());
}
ArrayList prev=new ArrayList();
用于(横幅列表:横幅列表){
Location Location=bannerList.getLocation();
如果(!prev.contains(位置)){
location.setLocationName(“1-”+location.getLocationName());
前添加(位置);
}
log.info(“位置”+bannerList.getId()+”:“
+bannerList.getLocation().getLocationName());
}

就我而言,它运行良好,我认为横幅或位置类都存在问题

看看这个

public class Test1 {

    public static  void main(String[] args){

        List<Banner> bannerList = generateBanners();

        for(Banner banner : bannerList){
            banner.getLocation().setLocationName("1 - " + banner.getLocation().getLocationName());
            System.out.println("ID : "+banner.getLocation().getLocationId() +"   Value : "+banner.getLocation().getLocationName());
        }

    }


    public static List<Banner> generateBanners(){
        List<Banner> banners = new ArrayList<Banner>();

        Location location = new Location(1,"LOCATIONA");
        Banner banner = new Banner(location);
        banners.add(banner);

        location = new Location(2,"LOCATIONA");
        banner = new Banner(location);
        banners.add(banner);

        location = new Location(3,"LOCATIONA");
        banner = new Banner(location);
        banners.add(banner);

        return banners;
    }

}
我的位置课

public class Location{

    private int locationId;
    private String locationName;

    public Location(){}

    public Location(int locationId, String locationName){
        this.locationName = locationName;
        this.locationId = locationId;
    }

    public int getLocationId() {
        return locationId;
    }

    public void setLocationId(int locationId) {
        this.locationId = locationId;
    }

    public String getLocationName() {
        return locationName;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }
}
输出:
如果有什么问题,请纠正我。

就我而言,它工作得很好。我认为横幅或位置类都有问题

看看这个

public class Test1 {

    public static  void main(String[] args){

        List<Banner> bannerList = generateBanners();

        for(Banner banner : bannerList){
            banner.getLocation().setLocationName("1 - " + banner.getLocation().getLocationName());
            System.out.println("ID : "+banner.getLocation().getLocationId() +"   Value : "+banner.getLocation().getLocationName());
        }

    }


    public static List<Banner> generateBanners(){
        List<Banner> banners = new ArrayList<Banner>();

        Location location = new Location(1,"LOCATIONA");
        Banner banner = new Banner(location);
        banners.add(banner);

        location = new Location(2,"LOCATIONA");
        banner = new Banner(location);
        banners.add(banner);

        location = new Location(3,"LOCATIONA");
        banner = new Banner(location);
        banners.add(banner);

        return banners;
    }

}
我的位置课

public class Location{

    private int locationId;
    private String locationName;

    public Location(){}

    public Location(int locationId, String locationName){
        this.locationName = locationName;
        this.locationId = locationId;
    }

    public int getLocationId() {
        return locationId;
    }

    public void setLocationId(int locationId) {
        this.locationId = locationId;
    }

    public String getLocationName() {
        return locationName;
    }

    public void setLocationName(String locationName) {
        this.locationName = locationName;
    }
}
输出:
如果有问题,请更正。

什么是
横幅
?此处:
横幅.getLocation().getLocationName()
这是一个输入错误吗?哎呀,对不起。这是输入错误,我修改了问题。谢谢。我认为这种行为的原因是所有的横幅实际上都指向同一个位置对象,而不仅仅是一个具有相同位置的对象name@parakmiakos:是的,这是真的,所有的横幅都指向同一个位置对象,但每个横幅都有它的Lo阳离子对象(虽然相同),不是吗?请参阅我的答案:)如果它是相同的对象,则只需更新一次即可
横幅是什么?这里:
banner.getLocation().getLocationName()
这是打字错误吗?哎呀,对不起。。是打字错误,我修改了问题。。ThanksI认为这种行为的原因是所有的横幅实际上都指向相同的位置对象,而不仅仅是具有相同位置的对象name@parakmiakos:是的,这是真的,所有的横幅都指向相同的位置对象,但每个横幅都有它的位置对象(尽管相同),是吗?请看我的答案:)如果它是同一个对象,你只需要在它相同时更新它,请看修改后的问题。是那样的吗