Results 1 to 11 of 11
  1. #1
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    90
    Rep Power
    0

    Default An array changed in a method does not change in another method

    Java Code:
    import java.io.*;
    public class Separate
    {
        int i,j,k;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int a[] = new int [15];
        int e[]= new int [15];
        int o[]= new int [15];
        public void accept()throws IOException
        {
            System.out.println( "Enter the numbers" );
            for( i = 0; i < 15; i++ )
            {
                a[i] = Integer.parseInt( br.readLine( ));
            }
        }
    
        public void split()
        {
            j = 0;
            k = 0;
            for( i = 0; i < 15; i++)
            {
                if( a[i] % 2 == 0 )
                {
                    e[j] = a[i];
                    j++;
                }
                else
                {
                    o[k] = a[i];
                    k++;
                }
            }
        }
        
        public void display( )
        {
            System.out.println( "Even number array");
            for( i = 0; i < j; i++)
            {
                System.out.println(e[j] );
            }
            System.out.println( "Odd number array" );
            for( i = 0; i < k; i++ )
            {
                System.out.println(o[k]);
            }
        }
    
        public static void main()throws IOException
    
        {
            Separate obj = new Separate( );
            obj.accept( );
            obj.split ( );
            obj.display( );
        }
    
    }
    I know that arrays are passed by value so the the changes I do in the split() function does not change in the display() method.
    What should I do to make this program work ?
    Thanks.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: An array changed in a method does not change in another method

    Can you post the program's output that shows what you are talking about?
    Add code that prints out the arrays' contents before it is changed and after it is changed.

    The Arrays class's to String method is useful for printing an array's contents when debugging code:
    Java Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    arrays are passed by value
    Where are the arrays passed in the posted code? They appear to be class fields that are accessed directly from the methods.
    Last edited by Norm; 06-11-2015 at 02:09 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    90
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    Output :
    Java Code:
    Enter the numbers
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Even number array
    0
    0
    0
    0
    0
    0
    0
    Odd number array
    0
    0
    0
    0
    0
    0
    0
    0
    So I am changing the array e[] and o[] in the split() method and trying to print it in the display() method but all I get is 0's
    Last edited by harshit shah; 06-11-2015 at 05:55 PM.

  4. #4
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    I think it's because you haven't initialized the ' j ' and ' k' variables in the display method.
    you've only done that in the split method (so the values of j and k will only be stored inside the split method and they can't be used outside of it).

    and the display method is faulty, shouldn't you tell it to print o[i] and e[i], because i is the that you increase
    now you basically keep telling it to keep printing the same value, while increase i as long as it's lower than a non-existent number.

    (I think, I'm pretty much an amateur myself :P)
    Last edited by Artemia; 06-11-2015 at 06:11 PM.

  5. #5
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    90
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    Quote Originally Posted by Artemia View Post
    and the display method is faulty, shouldn't you tell it to print o[i] and e[i], because i is the that you increase
    now you basically keep telling it to keep printing the same value, while increase i as long as it's lower than a non-existent number.

    (I think, I'm pretty much an [s]amateur[/s] myself :P)
    Thanks a ton.
    The problem is solved.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: An array changed in a method does not change in another method

    Quote Originally Posted by Artemia View Post
    I think it's because you haven't initialized the ' j ' and ' k' variables in the display method.
    you've only done that in the split method (so the values of j and k will only be stored inside the split method and they can't be used outside of it).
    Doesn't matter where the values are set, as they are attributes of the object, so the value they are set to in split() is the value for them in display().

    Quote Originally Posted by Artemia View Post
    and the display method is faulty, shouldn't you tell it to print o[i] and e[i], because i is the that you increase
    now you basically keep telling it to keep printing the same value, while increase i as long as it's lower than a non-existent number.

    (I think, I'm pretty much an amateur myself :P)
    But this is well spotted and is the key to the bug.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    Doesn't matter where the values are set, as they are attributes of the object, so the value they are set to in split() is the value for them in display().
    I don't really understand what you mean by this, can you elaborate?
    I've always learned that variables that are initialized in a method can only be used in that method, or is it different here because they are declared at the beginning of the class?

  8. #8
    harshit shah is offline Member
    Join Date
    Jan 2013
    Location
    Kolkata,India
    Posts
    90
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    Quote Originally Posted by Artemia View Post
    or is it different here because they are declared at the beginning of the class?
    Yes..I've declared all the variables at the beginning of the class making them global variable and hence they are accessible to all methods.

  9. #9
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: An array changed in a method does not change in another method

    cool, good to know :)
    thanks :)

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: An array changed in a method does not change in another method

    There is a difference between being initialized and being declared.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: An array changed in a method does not change in another method

    I too suffer from that short-variable-names-syndrome (being a (sloppy) mathematician); here's a small tip: change those variable names to something longer and more meaningful; i.e. change j and k to nofEven and nofOdd; you won't make those mistakes you made very easily with those longer names (and you can always change them back again later after it all works, if you really want).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 14
    Last Post: 06-27-2014, 09:16 PM
  2. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  3. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  4. objects as parameters change in method
    By zcd in forum New To Java
    Replies: 0
    Last Post: 01-29-2012, 08:37 PM
  5. Array, method to change character places
    By aramiky818 in forum New To Java
    Replies: 6
    Last Post: 09-12-2011, 12:38 AM

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
  •