mrjimoy_05
mrjimoy_05

Reputation: 3568

Modify value in ArrayList

I have this code :

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());
}

Supposed that every locationName have the same value : LOCATIONA, but the id is different each other. The log resulted in something like :

It's like the modified field still called on the next repetition.

This is the structure (I omit unnecessary detail) :

Banner.java

...
private Location location;
...

Location.java

...
private String locationName;
...

What's wrong? I tried using for(int i = 0; i ... but same.

EDIT

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

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

Upvotes: 1

Views: 133

Answers (5)

Secondo
Secondo

Reputation: 451

On my Part it works well i think theres a problem either banner or location class.

look at this.

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;
    }

}

this is my Banner class

public class Banner{

    private Location location;

    public Banner(){}

    public Banner(Location location){
        this.location = location;
    }

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }

}

my Location class.

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;
    }
}

Output:

ID : 1   Value : 1 - LOCATIONA
ID : 2   Value : 1 - LOCATIONA
ID : 3   Value : 1 - LOCATIONA

correct me if theres something wrong..

Upvotes: 0

parakmiakos
parakmiakos

Reputation: 3020

I believe the reason for this behavior is that all the banners are actually pointing to the same Location object, not just an object with the same name.

This will take into account any locations you have already updated:

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());
}

Upvotes: 1

lloistborn
lloistborn

Reputation: 373

Replace the first arguments with variable, something like this happen because you just add a string with other string through the loop,

something like:

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

the result

1 +
1 + 1
1 + 1 + 1

modify to something like:

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

your code should be like this,

 counter = 0;
 for (Banner bannerList : bannerLists) {
      counter += 1;
      bannerList.getLocation().setLocationName(counter + " - " + banner.getLocation().getLocationName());
      log.info("LOCATION : " + bannerList.getLocation().getLocationName());
 }

variable counter will update through the loop, and update your id

Upvotes: 0

Eran
Eran

Reputation: 393771

You are changing the location name in each iteration of the loop, adding "1 - " to the beginning.

bannerList.getLocation().setLocationName("1 - " 
                     + bannerList.getLocation().getLocationName());

Therefore, if all banners in the list share the same location, in each iteration you would see a different name.

If you want the same location name printed for all banners, don't update the location name inside the loop.

Upvotes: 2

Elliott Frisch
Elliott Frisch

Reputation: 201409

You're trying to recursively set the locationName when you get it. I assume you want its' position. You can save the reference Location to a variable. All together, something like

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);
}

or

List<Banner> bannerLists = ...; // Get the list
for (int pos = 0, len = bannerLists.size(); pos < len; pos++) {
  Banner banner = bannerLists.get(pos);
  Location location = banner.getLocation();
  String locName = Integer.toString(pos);
  location.setLocationName(locName);
  log.info("LOCATION " + bannerList.getId() + " : " + locName);
}

Upvotes: 4

Related Questions