Results 1 to 5 of 5
  1. #1
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Angry Issue with setting values in an array

    I'm trying to write a genetic algorithm, I'm having a bit of trouble managing the arrays though.
    The method I'm writing is supposed to take a array of 16 binary digits (16 zeros or ones) and create ten new arrays that are each replicas of the original array with two random digits flipped (if it was a zero, it becomes a one), then returning all of the arrays in a multidimensional array.

    My problem is that whenever I modify one of the new arrays ( x=4; bit1=6; generation[x][bit1] = 1; ) it changes the state of all the arrays (even the original one!) at digit 6.

    Here's a simplified version of my code to illustrate the problem:

    Java Code:
    import java.util.Random;
    
    public class GeneticTestingSimplified{
        public static void main(String[] args){
            int closestMatch[] = {0,1,0,0,1,1,0,1,1,0,1,1,0,1,1,1};
            System.out.println("The input array:");
            for(int i=0;i<16;i++){
                System.out.print(closestMatch[i]);
            }
    
            int generation[][] = mutate(closestMatch);
    
    
            System.out.println("\nThe same array after calling mutate():"); 
            for(int i=0;i<16;i++){
                System.out.print(closestMatch[i]);
            }
            //note that this is different than the original closestMatch array, 
            //even though mutate() should not have been able to modify it
            
            System.out.println("\nOutput array of mutate():");
            for(int x=0;x<10;x++){
                for(int y=0;y<16;y++){
                    System.out.print(generation[x][y]);
                }
                System.out.print("\n");
            }
        }
    
    
        public static int[][] mutate(int closestMatch[]){
        	Random rand = new Random();
        	int generation[][] = new int[10][16];
                for(int i=0;i<10;i++){
                    generation[i] = closestMatch;
                }
    
        	for(int x=1;x<10;x++){
        		int bit1 = rand.nextInt(16);
                if(generation[x][bit1] == 0){
                    generation[x][bit1] = 1;
                }
                else generation[x][bit1] = 0;
                int bit2 = rand.nextInt(16);
                while(bit1 == bit2){
                    bit2 = rand.nextInt(16);
                }
                if(generation[x][bit2] == 0){
                    generation[x][bit2] = 1;
                }
                else generation[x][bit2] = 0;
        	}
        	return generation;
        }
    }

    Why are all of my arrays being changed? Mutate shouldn't be able to modify closestMatch[], how is this happening!?
    How can I fix this code!?!?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Issue with setting values in an array

    Quote Originally Posted by pyat77 View Post
    Why are all of my arrays being changed?
    Because you wrote code to do that.
    Mutate shouldn't be able to modify closestMatch[]
    Incorrect.
    how is this happening!?
    Looks like you need to revise Objects and parameter passing.

  3. #3
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Issue with setting values in an array

    there a problem with the mutate method. Specifically here:
    Java Code:
     Random rand = new Random();
     int generation[][] = new int[10][16];
     for(int i=0;i<10;i++){
            generation[i] = closestMatch;
     }
    you can't just pass the array closestMatch into generation[i]; You should pass in each value of closestMatch into generation, one at a time with a second for loop.

  4. #4
    pyat77 is offline Member
    Join Date
    Feb 2010
    Posts
    15
    Rep Power
    0

    Default Re: Issue with setting values in an array

    Thank you CHLim! Can you please explain to me why this is? Or, why I was getting those results?

  5. #5
    CHLim is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Issue with setting values in an array

    Well pyat, i just messed around with the code and kinda got it to work. Why it didn't work i'm not sure but i'm guessing that there's an error that was not detected. But if you ever find out why, do let me know. Thanks.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  2. Setting up a String Array
    By AJArmstron@aol.com in forum New To Java
    Replies: 12
    Last Post: 04-16-2010, 01:42 PM
  3. Compare Array and setting to temp
    By saqib15 in forum New To Java
    Replies: 0
    Last Post: 02-17-2010, 07:16 PM
  4. setting and getting array
    By jgonzalez14 in forum New To Java
    Replies: 6
    Last Post: 11-26-2008, 07:54 AM
  5. setting format for FLoat values
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 02:22 PM

Tags for this Thread

Posting Permissions

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