Results 1 to 13 of 13
  1. #1
    selimzz is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default very Strange behaviour !!

    I've faced a very strange issue in java ,no one could describe to me why does java do that !!!so i would plz to understand this issue if anyone could

    I've 2 classes City and User

    and City has arrayList of users

    if i have in my program arraylist of City(say cities) and i want to change property in specific user object (has say 'x' index) in the arraylist of specific object of city in cities
    the strange behaviour is that : the user which has index 'x' will be changed in all City objects in cities not the spicific one


    Code:

    for(City city:cities)
    {
    city.setUsers(users);
    }

    cities.get(0).getUsers().get(0).setCity_id(3);
    System.out.println(cities.get(1).getUsers().get(0) .getCity_id());


    although i wrote cities.get(1) ,it'll print 3 not 0 but if i change getUsers().get(0) to get(1) will print 0 !!!!!!!!!!!!!!!!!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: very Strange behaviour !!

    Please make a small, complete program (paste it here) that compiles, executes and shows the problem. Run the program, copy its output and paste it here and add some comments to show what is wrong with the output and what it should be.

    Be sure to wrap the code in code tags. http://www.java-forums.org/misc.php?do=bbcode#code
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: very Strange behaviour !!

    Quote Originally Posted by selimzz View Post
    for(City city:cities)
    {
    city.setUsers(users);
    }
    That snippet sets the same (ArrayList) of users to each city ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    selimzz is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: very Strange behaviour !!

    yeah i thought about it but that it isn't the problem as i set different arraylist from different objects of users to each city and it was the same result :s

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: very Strange behaviour !!

    Can you make a SSCCE for testing? Short, Self Contained, Correct Example
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: very Strange behaviour !!

    selimzz, please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: very Strange behaviour !!

    Quote Originally Posted by selimzz View Post
    yeah i thought about it but that it isn't the problem as i set different arraylist from different objects of users to each city and it was the same result :s
    Show us some of your actual code; I'm sure it's something similar as I pointed out in my previous reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    selimzz is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: very Strange behaviour !!

    Quote Originally Posted by JosAH View Post
    Show us some of your actual code; I'm sure it's something similar as I pointed out in my previous reply.

    kind regards,

    Jos
    here The actual Code:

    Java Code:
    import java.util.ArrayList;
    
    	class User
    	{
    		private boolean activeFlag;
    
    		public boolean isActiveFlag() {
    			return activeFlag;
    		}
    
    		public void setActiveFlag(boolean activeFlag) {
    			this.activeFlag = activeFlag;
    		}
    		
    		
    	}
        class City
        {
        	private String name;
        	private ArrayList<User>users;
        	public City(String name,ArrayList<User>users)
        	{
        		this.name=name;
        		this.users=users;
        	}
    		public String getName() {
    			return name;
    		}
    		public void setName(String name) {
    			this.name = name;
    		}
    		public ArrayList<User> getUsers() {
    			return users;
    		}
    		public void setUsers(ArrayList<User> users) {
    			this.users = users;
    		}
        }
    	/**
    	 * @param args
    	 */
        public class Test {
    	public static void main(String[] args) {
    		
    		User u1=new User();
    		User u2=new User();
    		User u3=new User();
    		User u4=new User();
    		ArrayList <User>users1=new ArrayList<User>();
    		users1.add(u1);users1.add(u2);
    		ArrayList <User>users2=new ArrayList<User>();
    		users2.add(u3);users2.add(u4);
    		City city1=new City("Tokyo",users1);
    		City city2=new City("London",users2);
    		ArrayList<City> cities=new ArrayList<City>();
    		cities.add(city1);cities.add(city2); 
    		cities.get(0).getUsers().get(0).setActiveFlag(true);
    		System.out.println(cities.get(0).getUsers().get(0).isActiveFlag());//true :s!! why !!!
    		System.out.println(cities.get(0).getUsers().get(1).isActiveFlag());// false as expected because of getUsers().get(1)
    		
    
    	}
    
    }
    Last edited by JosAH; 01-06-2013 at 12:02 PM. Reason: added [code] ... [/code] tags

  9. #9
    selimzz is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: very Strange behaviour !!

    I did ,it will be in mind next time :)

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: very Strange behaviour !!

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: very Strange behaviour !!

    Quote Originally Posted by selimzz View Post
    Java Code:
    		cities.get(0).getUsers().get(0).setActiveFlag(true);
    		System.out.println(cities.get(0).getUsers().get(0).isActiveFlag());//true :s!! why !!!
    The active flag is true becaue you have set it so the line before; nothing strange about it ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    selimzz is offline Member
    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    Default Re: very Strange behaviour !!

    Quote Originally Posted by JosAH View Post
    The active flag is true becaue you have set it so the line before; nothing strange about it ...

    kind regards,

    Jos
    hahaha sorry i mean System.out.println(cities.get(1).getUsers().get(0) .isActiveFlag());

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,777
    Blog Entries
    7
    Rep Power
    21

    Default Re: very Strange behaviour !!

    I just ran your code and it returned false (as expected). Did you recompile your code before testing it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Strange JFrame behaviour
    By javaExprt in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 04:20 AM
  2. Strange behaviour
    By imadabh in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-11-2011, 04:31 PM
  3. Strange JVM behaviour
    By pjpr in forum Advanced Java
    Replies: 13
    Last Post: 01-03-2011, 08:39 PM
  4. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 01:03 PM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 10:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •