Results 1 to 13 of 13
Thread: very Strange behaviour !!
- 01-04-2013, 07:34 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
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 !!!!!!!!!!!!!!!!!
- 01-04-2013, 07:42 PM #2
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#codeIf you don't understand my response, don't ignore it, ask a question.
- 01-04-2013, 08:11 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-05-2013, 12:09 AM #4
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
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
- 01-05-2013, 01:33 AM #5
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.
- 01-05-2013, 09:19 AM #6
Re: very Strange behaviour !!
selimzz, please go through the Forum Rules -- particularly the third paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-05-2013, 09:56 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-06-2013, 10:33 AM #8
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
Re: very Strange behaviour !!
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 11:02 AM. Reason: added [code] ... [/code] tags
- 01-06-2013, 10:36 AM #9
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
Re: very Strange behaviour !!
I did ,it will be in mind next time :)
- 01-06-2013, 10:41 AM #10
Re: very Strange behaviour !!
Why do they call it rush hour when nothing moves? - Robin Williams
- 01-06-2013, 11:05 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
- 01-06-2013, 11:13 AM #12
Member
- Join Date
- Jan 2013
- Posts
- 5
- Rep Power
- 0
- 01-06-2013, 11:35 AM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Strange JFrame behaviour
By javaExprt in forum New To JavaReplies: 5Last Post: 12-24-2012, 03:20 AM -
Strange behaviour
By imadabh in forum Threads and SynchronizationReplies: 1Last Post: 05-11-2011, 03:31 PM -
Strange JVM behaviour
By pjpr in forum Advanced JavaReplies: 13Last Post: 01-03-2011, 07:39 PM -
Strange behaviour in serialization
By Wolverine in forum NetworkingReplies: 0Last Post: 05-23-2009, 12:03 PM -
Strange behaviour in swing
By cbalu in forum AWT / SwingReplies: 1Last Post: 05-23-2008, 09:23 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks