Results 1 to 14 of 14
Thread: Why Java changes my variables..?
 12172010, 11:41 PM #1Member
 Join Date
 Dec 2010
 Location
 Germany, Dortmund
 Posts
 5
 Rep Power
 0
Why Java changes my variables..?
Hello everybody,
I am one of the new comers to Java....
I have faced a stupid problem. I am implementing some genetic algorithm in Java. In one part I save a 1D array that is a member of a bigger 2D array in another array. Then I need to make some changes in this big 2D array, but these changes should not affect the thing that I have stored before in somewhere else, BUT it affects!!!
To make the situation more clear, look at this example:
...
Solution[i] = pop[0][i];
.....
pop[0] = Mutate(pop[0],pop[1]);
......
Now if I read Solution[], it should be the thing that I have stored before, but the changes in pop exactly affect this Solution array....
Could someone please tell me why this happens?
 12182010, 01:02 AM #2
Can you show the full code? It's almost impossible to tell what the problem is from this little bit of information.
 12182010, 01:21 AM #3Member
 Join Date
 Dec 2010
 Location
 Germany, Dortmund
 Posts
 5
 Rep Power
 0
ok, here is the relevant parts of the code:
.....
while (pc <= iteration ){
...
for(int i = 0; i < 100; ++i)
if( s_tardiness[0][i] + s_tardiness[1][i] < min){
min = s_tardiness[0][i] + s_tardiness[1][i];
solution[0] = pop[0][i]; //Here I store
solution[1] = pop[1][i]; //and here
}
......
Attach x = new Attach();
x = instance.Mutate2(offspring[0], offspring[1]); //here is the change for the // big array
pop[0] = x.first; // Here unwanted change happens
pop[1] = x.second; // And here
++pc;
}
I will be very grateful if give me an idea
 12182010, 02:15 AM #4Senior Member
 Join Date
 Apr 2010
 Location
 Philippines
 Posts
 580
 Rep Power
 5
Have some question.
How did you give value to offspring?
where did you get offspring?
What is your input? and what is the output?
Does it return compile error?
I cannot help you because the information or details that you gave is limited.
 12182010, 03:32 AM #5
@OP, I hate to sound this way... but you do realize that by holding back this type of information, you're preventing us from helping you... right? I can't see any problems with what you've posted, but I can't run it to test. Without the entire code (as in the whole thingincluding methods that are called; when I copypaste it into a new file, it should compile or at least provide the same compile errors that you receive, if any), I can't help you.
 12182010, 01:55 PM #6Member
 Join Date
 Dec 2010
 Location
 Germany, Dortmund
 Posts
 5
 Rep Power
 0
ok, if you want the whole code, it is here, I thought may be the relevant parts are enough....
Java Code:import java.util.Arrays; import java.util.Random; import java.util.Scanner; public class mSTj { /** * @param args */ int[] Process = {29,32,35,4,49,4,15,24,41,9,2,48,20,7,42,43,39,45,35,33,22,25,24,30,37,27,16,21,17,14,22,6,8,44,29,6,32,25,43,47,17,48,37,5,19,22,50,40,28,32,5,34,2,28,50,14,17,50,34,11,29,18,3,46,13,2,7,13,20,47,2,19,9,2,44,49,30,19,46,32,24,45,9,41,46,40,41,31,46,2,12,48,33,26,32,25,31,6,12,1}; int[] Due = {112,82,87,27,68,97,110,80,104,72,32,139,23,24,97,54,43,78,128,104,86,34,48,63,78,38,88,105,88,89,96,8,67,114,30,59,116,31,82,99,100,141,91,52,71,117,57,57,106,105,77,103,39,32,140,15,109,85,57,48,88,63,61,101,77,91,62,111,60,81,53,97,68,71,58,148,114,23,54,41,57,62,15,140,116,112,65,110,126,19,51,86,113,63,88,66,128,76,88,16}; //int[] Process = {4 ,6 ,5 ,4 ,8 ,4 ,8 ,9 ,7 ,5}; //int[] Due = {9 ,7 ,5 ,8 ,11,13,13,11,8,12}; int[] Duesort(int[] Array){ int[] Result = new int[Array.length]; for(int i = Array.length ; i > 0; i){ int temp = Array[Array.length  1]; Result[i 1] = Array.length  1; for(int j = Array.length  1; j > 0; j) if(Array[j  1] > temp){ temp = Array[j  1]; Result[i  1] = j  1; } Array[Result[i  1]] = 0; } return Result; } /****************************************************************************/ /****************************************************************************/ boolean Isthere(int [] Array,int n){ int a = 0; int i = 0; while (a == 0 & i < Array.length){ if(Array[i] == n) a = 1; ++i; } if (a == 1) return true; else return false; } /****************************************************************************/ int Isthereindex(int [] Array,int n){ int a = 0; int i = 0; while (a == 0 & i < Array.length){ if(Array[i] == n) break; ++i; } return i; } /****************************************************************************/ boolean Isthere2(int [] Array,int n, int lim){ int a = 0; int i = 0; while (a == 0 & i < lim){ if(Array[i] == n) a = 1; ++i; } if (a == 1) return true; else return false; } /******************************************************************************/ boolean find(int[] Array, int num, int lim){ for (int i = 0; i < lim; ++i){ if (Array[i] == num) return true; } return false; } /******************************************************************************/ int[] Completion(int[] Array){ int[] C = new int[Array.length]; C[0] = this.Process[Array[0]]; for (int i = 1; i < Array.length ; ++i) C[i] = C[i1] + this.Process[Array[i]]; return C; } ////////////////////////////////////////////////////////////////////////////////////// int[] Tardiness(int[] Array){ int[] T = new int[Array.length]; int[] C = this.Completion(Array); int d; mSTj ins = new mSTj(); for (int i = 0; i < Array.length; ++i){ d = C[i]  ins.Due[Array[i]]; if (d > 0) T[i] = d; else T[i] = 0; } return T; } ////////////////////////////////////////////////////////////////////////////////// int Sum_tardiness(int[] Array){ //test instance = new test(); int sum = 0; int[] Tardiness = this.Tardiness(Array); for(int i=0; i < Array.length ; ++i) sum += Tardiness[i]; return sum; } /*****************************************************************************/ // Doesn't work properly, number of nunzero entries of Array must be considered int Rand(int a, int b, int[] Array){ //generate an integer random number between a and b, but not equal to the entries of Array int[] inter = new int[b  a + 1  Array.length]; int j = 0; for(int i = a; i <= b; ++i) if(this.Isthere(Array, i)==false){ inter[j] = i; ++j; } Random r = new Random(); int x = r.nextInt(inter.length); return inter[x]; } /*****************************************************************************/ int[] Combine(int[] Array1,int[] Array2){ int[] c = new int[7]; c[0] = 0; c[6] = Array1.length  1; //We suppose both arrays have the same length for(int j= 1; j < 6; ++j){ int a = 0; Random rand = new Random(); a = rand.nextInt(Array1.length); if(this.Isthere2(c, a, j) == false) c[j] = a; } Arrays.sort(c); //test loc = new test(); //int[] c2 = new int[7]; //c2[0] = 0; // It is necessary for the next step! //c2[6] = 100; // It is necessary for the next step! //for(int i = 0; i < 5; ++i){ // c2[i + 1] = loc.Minfind(c); // c[loc.Minindexfind(c)] = 1000000; // } int[] offspring = new int[Array1.length]; offspring[0] = Array1[0]; for(int i = 1; i < 7; ++i) for(int j = c[i  1] + 1; j <= c[i]; ++j) if(i%2 == 1){ if(this.Isthere2(offspring, Array1[j], j) == false) offspring[j] = Array1[j]; else if(this.Isthere2(offspring, Array2[j], j)== false) offspring[j] = Array2[j]; else offspring[j] = 1; } else{ if(this.Isthere2(offspring, Array2[j], j) == false) offspring[j] = Array2[j]; else if(this.Isthere2(offspring, Array1[j], j)== false) offspring[j] = Array1[j]; else offspring[j] = 1; } int[] filter = new int[Array1.length]; int j = 0; for(int i = 0; i < Array1.length; ++i) if(this.Isthere(offspring, Array1[i]) == false ){ filter[j] = Array1[i]; ++j; } j; if(j != 1) for(int k = 0; k < offspring.length; ++k) if(offspring[k] == 1){ offspring[k] = filter[j]; j; } return offspring; } //////////////////////////////////////////////////////////////////////////////////// int[] Mutate1(int[] Array){ Random rand = new Random(); int a = rand.nextInt(Array.length); int b = a; while(b == a) b = rand.nextInt(Array.length); int temp; temp = Array[a]; Array[a] = Array[b]; Array[b] = temp; return Array; } /*******************************************************************************/ Attach Mutate2(int[][] Array1,int[][] Array2){ Random rand = new Random(); int a = rand.nextInt(Array1[0].length); //Suppose both arrays have the same length int b = a; while(b == a) b = rand.nextInt(Array1[0].length); int temp1 = Array1[0][a]; int temp2 = Array2[0][b]; for(int i = 0; i < Array1.length; ++i){ if(this.Isthere(Array1[i], temp1) == true) Array1[i][this.Isthereindex(Array1[i], temp1)] = temp2; if(this.Isthere(Array2[i], temp2) == true) Array2[i][this.Isthereindex(Array2[i], temp2)] = temp1; } Attach x = new Attach(); x.first = Array1; x.second = Array2; for(int i = 0; i < Array1.length; ++i) if(this.HaveCommon(Array1[i], Array2[i]) == true) System.out.println("*********** Mutation2***********"); return x; } /*******************************************************************************/ int Minfind(int[] Array){ int temp = 1000000; for(int i = 0; i < Array.length; ++i) if(Array[i] < temp) temp = Array[i]; return temp; } /********************************************************************************/ boolean HaveCommon(int[] Array1, int Array2[]){ boolean c = false; for(int i = 0; i < Array1.length; ++i) for(int j = 0; j < Array2.length; ++j) if(Array2[j] == Array1[i]) c = true; if (c == true) return true; else return false; } /********************************************************************************/ int Minindexfind(int[] Array){ int temp = 0; for(int i = 1; i < Array.length; ++i) if(Array[i] < Array[temp]) temp = i; return temp; } /****************************************************************************/ public static void main(String[] args) { // TODO Autogenerated method stub mSTj instance = new mSTj(); int[] n = new int[instance.Due.length]; n = instance.Duesort(instance.Due); int[][] m = new int[2][instance.Due.length / 2]; for(int i = 0; i < instance.Due.length / 2; ++i){ m[0][i] = n[2 * i]; m[1][i] = n[2 * i + 1]; } //generate initial population int[][][] pop = new int[2][100][instance.Due.length / 2]; Random randnum = new Random(); for (int i = 0; i < 2; ++i ) //generate 100 possible solution as initial population for(int j = 0; j < 100; ++j){ int a; int k = 0; while(k < instance.Due.length / 2){ a = randnum.nextInt(instance.Due.length / 2); // remember 0 <= m <= 49 if (instance.Isthere2(pop[i][j], m[i][a], k) == false){ pop[i][j][k] = m[i][a]; ++k; } } } int[][] s_tardiness = new int[2][100]; //Each element of this array will contain sum of tardiness for each member of pop1 and pop2 int[][] solution = new int[2][instance.Due.length / 2]; int[] minschtardiness = {10000000,10000000}; int[][][] offspring = new int[2][100][instance.Due.length / 2]; int temp; int[] bst = {1,1}; //indicates in which iteration the best solution has occurred int iteration = 10; //How many times this loop should be repeated (termination condition) int min = 20000001; int index = 0; //keeps the index of the minimum 'Sum of tardiness' for each machine System.out.println("# iteration\t\t" + "Sum of Tardinessin machine 1\t\t" + "Sum of Tardinessin machine 1"); System.out.println(""); int pc = 1; //Scanner in = new Scanner(System.in); while (pc <= iteration ){ //we will repeat our algorithm "iteration" times and then report the best solution during this iterations. //may be we will repeat 1000 times, but the best solution has occurred in iteration number 123, then //we will report the solution that has obtained in iteration no. 123 System.out.println(min); for (int i = 0; i < 2; ++i) for(int j = 0; j < 100; ++j) s_tardiness[i][j] = instance.Sum_tardiness(pop[i][j]); //int p = 0; //int r = 0; // Tournament Selection int[] sel = new int[2]; int[] tar = new int[2]; int[][] parents = new int[2][20]; for(int i = 0; i < 2; ++i){ int t = 0; while( t < 20){ for(int j = 0; j < 2; ++j){ sel[j] = randnum.nextInt(100); tar[j] = instance.Sum_tardiness(pop[i][sel[j]]); //****************************** } temp = instance.Minindexfind(tar); if(instance.Isthere(parents[i], sel[temp]) == false){ parents[i][t] = sel[temp]; ++t; } } } //Combination & Mutation for(int i = 0; i < 2; ++i) for(int j = 0; j < 20; ++j) offspring[i][j] = pop[i][parents[i][j]]; //********************************************* for(int j = 20; j < 100; ++j){ // Now produce 80 offspring, you have the function! for(int i = 0; i < 2; ++i){ int a = randnum.nextInt(20); int b = a; while(b == a) b = randnum.nextInt(20); offspring[i][j] = instance.Combine(pop[i][parents[i][a]], pop[i][parents[i][b]]); offspring[i][j] = instance.Mutate1(offspring[i][j]); } } Attach x = new Attach(); x = instance.Mutate2(offspring[0], offspring[1]); pop[0] = x.first; //************************ pop[1] = x.second; for(int i = 0; i < 100; ++i) if( s_tardiness[0][i] + s_tardiness[1][i] < min){ //r = in.nextInt(); min = s_tardiness[0][i] + s_tardiness[1][i]; solution[0] = pop[0][i]; solution[1] = pop[1][i]; //System.out.println("********************************"); //for(int t = 0; t < solution[0].length; ++t) //System.out.println(solution[0][t]+"\t\t\t"+solution[1][t]); //System.out.println("********************************"); //if(instance.HaveCommon(solution[0], solution[1]) == true) //System.out.println("*************"+ pc); } //System.out.println("********************************"); //for(int t = 0; t < solution[0].length; ++t) //System.out.println(solution[0][t]+ "\t\t\t" + solution[1][t]); //System.out.println("********************************"); ++pc; } System.out.println(""); System.out.println("\n\nFinal Schedule:"); System.out.println(""); for (int i = 0; i < pop[0][0].length; ++i) System.out.println(solution[0][i] + "\t\t\t" + solution[1][i]); System.out.println("\n"); System.out.println(min); //******************************************************************************* //STjSCj q = new STjSCj(); //int[] w = {1,2,3,4,5,6,7}; //int s = q.Rand(1, 15, w); //for(int i = 0; i < 100; ++i) //System.out.println(i + "\t" + m[i]); } }

Just 2 points. First, the original data are Process[] and Due[] with 100 entries. But for the convenience in debugging I have produced the small Process[] and Due[] with just 10 entries.
Second, you need a class with the name "Attach". It should have this definition:
Java Code:public class Attach { int[][] first; int[][] second; }
Last edited by Fubarable; 12182010 at 02:22 PM. Reason: Moderator Edit: Code tags added

Arrays are reference variables and as such, if you make changes to the original object that is referred to by both, you'll see the changes in both array variables. What happens if you assign solution a clone of the array?
Java Code:Solution[i] = pop[0][i].clone();
For instance, to greatly simplify your problem,
Java Code:import java.util.Arrays; public class MSTjSimple { public static void main(String[] args) { int[][][] pop = { {{1, 2, 3}, {3, 4, 5}}, {{5, 6, 7}, {7, 8, 9}}}; int[][] solution = new int[pop[0].length][]; for (int i = 0; i < pop[0].length; i++) { // solution[i] = pop[0][i].clone(); // ***** uncomment this solution[i] = pop[0][i]; // ****** comment this line } System.out.println("before modification"); System.out.println("solution: " + Arrays.deepToString(solution)); System.out.println(); pop[0] = mutate(pop[0]); System.out.println("after modification"); System.out.println("solution: " + Arrays.deepToString(solution)); } private static int[][] mutate(int[][] p0) { p0[0][0] = 100; int[][] result = new int[p0.length][p0[0].length]; return result; } }
Last edited by Fubarable; 12182010 at 02:52 PM.
 12182010, 03:25 PM #8Member
 Join Date
 Dec 2010
 Location
 Germany, Dortmund
 Posts
 5
 Rep Power
 0

I'm glad you've got it working, and it should as long as you understand that clone will only do a shallow clone.
In the next lines of your code, I am really feeling the power of Java over C++...
Java is great, don't get me wrong, but there's nothing that is done in Java that can't be done in C++. The converse is certainly not true.
 12192010, 04:57 PM #10Member
 Join Date
 Dec 2010
 Location
 Germany, Dortmund
 Posts
 5
 Rep Power
 0
[QUOTE=Fubarable;
I'm not sure I'd ever say that. :)
Java is great, don't get me wrong, but there's nothing that is done in Java that can't be done in C++. The converse is certainly not true.[/QUOTE]
Correct. I mean how easy is to do the same thing in Java or C++....
 12192010, 05:27 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,535
 Blog Entries
 7
 Rep Power
 20
I find that an extremely dangerous statement; basically you're saying that there are things that can be done in C++ while it would be impossible to do in Java. Apart from close to the bare metal fiddling, care to give an example? Or is that what you were talking about?
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass

Yes, that's exactly what I'm talking about, and in many situations, that is precisely what is necessary, that and C/C++'s wonderful ability to allow you to shoot yourself in the foot without much effort. But myself, I like the power and safety of Java and am satisfied to do most of my coding in Java. If I need to get closer to the metal, then I've had good results with very small C programs (when I've been able to debug the suckers and get rid of farkin' pointer and memory errors) or AutoIt joined to Java via JNA or Runtime.exec.
Last edited by Fubarable; 12192010 at 06:05 PM.
 12192010, 06:32 PM #13
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,535
 Blog Entries
 7
 Rep Power
 20
OK, we're talking the same thing then; one small remark about that shooting yourself in the foot: that's C, it can easilly shoot you in the foot; C++ also stabs you in the back and slices your throat in two or three incorrectly placed characters if you don't pay attention ;) Especially that darn operator overloading is evil. Java does a good job in keeping your intentions clean and it doesn't do much behind your back.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 12202010, 05:35 AM #14
Similar Threads

java script variables with jsp
By meghana in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 08182010, 12:36 PM 
reading values into variables using java
By kskgupta in forum New To JavaReplies: 2Last Post: 05162010, 05:48 AM 
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09092009, 05:48 PM 
Help with variables in java
By fernando in forum New To JavaReplies: 2Last Post: 08062007, 05:03 PM 
Problem with variables in java
By carl in forum AWT / SwingReplies: 1Last Post: 07312007, 07:50 PM
Bookmarks