Results 1 to 11 of 11
  1. #1
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default passing an array between classes

    Hi,

    Ok when i pass my array to another class it modifies the array. i am not sure why. I get the value of the array from a class and then i pass the value of the array to a different class. when i then modify the values in the class it changes the original values of the array. Hope that makes sense.

    Many thanks

    Gisler

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    It's probably because the array you think to modify actually points to the original array. What you should do is use a loop (or System.arraycopy) to copy all values in the original array to the array to be modified.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Couple of things...
    First you say that you are passing an array between classes:
    when i pass my array to another class it modifies the array
    Then you say you are passing an array value between classes:
    I get the value of the array from a class and then i pass the value of the array to a different class.
    Which is it?
    I would suggest you post some code showing what you are trying to do.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by gisler View Post

    when i then modify the values in the class it changes the original values of the array.

    Gisler
    Did you mean ...you are changing values of some other variables other than array or changing values of array?

  5. #5
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    Hi, thanks for the quick reply. Here is some of my code

    randomNumbers = newGame.getarray(); //gets the array
    totalGuesses++;
    guessNumbers[0]= guessN1.getNumber();
    guessNumbers[1]= guessN2.getNumber();
    guessNumbers[2]= guessN3.getNumber();
    guessNumbers[3]= guessN4.getNumber();
    results = calculateGuess.getGuessresults(guessNumbers ,randomNumbers);
    score=(results[2]/totalGuesses)+score;

    the first line gets the values of the array and puts it into a new array called randomNumbers. I then pass the values of the array randomNumbers and guessNumbers to a new class called calculateGuess. The methods for that class is...

    public int[] getGuessresults(int[] guessed,int [] randomNum)
    {
    results = calculateResults(guessed, randomNum);
    return results;
    }

    private int[] calculateResults(int[] guesses,int[]randomNums)
    {
    //Edit the array here.

    Thanks for your help.

    gisler

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    I'm still fuzzy about were the problem is. What array elements get changed? Have you tried to debug the problem (with printlns)? If not, try putting printlns throughout your code so you can follow the flow of data and find where it's being changed.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    the problem is when i change the values of the array in my calculateGuess class

    private int[] calculateResults(int[] guesses,int[]randomNums)
    {
    //Edit the array here.

    it also changes the values of the array in my newgame class.

    randomNumbers = newGame.getarray(); //gets the array
    I have tried debugging this load and can't figure it out. i think it might have something to do with making my methods or classes public when they should be private.

    Many thanks
    gisler

  8. #8
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Can you please specify where the values are getting changed in your main class?

  9. #9
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    One more thing...you can directly call calculateResults() method from your main class as nothing is done in getGuessresults() except you are calling calculateResults() method.

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Quote Originally Posted by Supamagier View Post
    It's probably because the array you think to modify actually points to the original array. What you should do is use a loop (or System.arraycopy) to copy all values in the original array to the array to be modified.
    this. arrays are objects. therefore, when you pass it along, you pass a reference to the original array. it's behaving how it is meant to behave.

  11. #11
    gisler is offline Member
    Join Date
    Apr 2009
    Posts
    26
    Rep Power
    0

    Default

    thanks emceenugget. that makes perfect sense. i will just copy the array to a new array and my problem should be solved. Also thanks Kbindumadhavi. I have gotten rid of the useless method.

    Thanks eveyone else.

    gisler

Similar Threads

  1. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 03:41 PM
  2. Passing values between classes problem.
    By alin_ms in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 07:49 PM
  3. Need help passing data between classes
    By bri1547 in forum New To Java
    Replies: 3
    Last Post: 07-21-2008, 05:19 AM
  4. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 PM
  5. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 10:35 AM

Posting Permissions

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