call by value for objects
-
hi all as u know java is using call by reference with objects how can i instead call by value for objects?? thanks in advance
Hi, You can't pass an object by value. The best approximation would be to pass a clone of the object. And why would you need to pass by value? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
Hi, You can't pass an object by value. The best approximation would be to pass a clone of the object. And why would you need to pass by value? :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
i have arraylist of objects and i wrote a method that take one object from the arraylist and return a modified object the problem is after calling the method all objects in the arraylist changed to the new value!!! when i debuge my program i realized that all objects (in the array list and the returning object) have the same id (i'm using eclipse), which is suppose to be unique and because of that any changes to object are applied to all version i don't know way can you help please??:rose:
-
i have arraylist of objects and i wrote a method that take one object from the arraylist and return a modified object the problem is after calling the method all objects in the arraylist changed to the new value!!! when i debuge my program i realized that all objects (in the array list and the returning object) have the same id (i'm using eclipse), which is suppose to be unique and because of that any changes to object are applied to all version i don't know way can you help please??:rose:
from what you told, but did not show, it sounds like you are only creating a single object, and have a loop that (1) modifies it somehow and (2) adds its reference to the arraylist, resulting in a large number of identical references to the single object in its final state. Check: is the "new Whatever(...)" inside or outside your loop? If that does not help, then I suggest you show the relevant code. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
from what you told, but did not show, it sounds like you are only creating a single object, and have a loop that (1) modifies it somehow and (2) adds its reference to the arraylist, resulting in a large number of identical references to the single object in its final state. Check: is the "new Whatever(...)" inside or outside your loop? If that does not help, then I suggest you show the relevant code. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm
ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); }
my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :) -
my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm
ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); }
my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :)Sorry, that code is hardly readable; please use PRE tags, not CODE tags, so we get indentation and good contrast. You can still edit the existing message. Furthermore, what needs to be checked is how the values or objects get created that you are adding to the arraylist, hence one has to inspect Mutation(), and whatever is used to create the entries in NPOP (as those will end up in TMP). :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.
-
my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm
ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); }
my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :)Your question is still not very clear because you didn't provide the function prototype (we can only see the content of the function, not the parameters which are passed). Anyway, in Java whenever you manipulate an object, it is similar as using a pointer to the object. So, when you pass the object to a function, you pass the pointer and not a copy of the object. If you really need to make changes in a copy of the object, then you have to create a new instance of the object yourself. There's no such thing as passing by value in java.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Your question is still not very clear because you didn't provide the function prototype (we can only see the content of the function, not the parameters which are passed). Anyway, in Java whenever you manipulate an object, it is similar as using a pointer to the object. So, when you pass the object to a function, you pass the pointer and not a copy of the object. If you really need to make changes in a copy of the object, then you have to create a new instance of the object yourself. There's no such thing as passing by value in java.
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++Luc Pattyn Cédric Moonen thank u all for your reply its clear now that's my problem was i don't create a new insantce of my objects but i referenced to existing object schedule s = POP.get(i); where POP is arraylist of schedules again thank u for ur help:rose::rose::rose:
-
hi all as u know java is using call by reference with objects how can i instead call by value for objects?? thanks in advance