Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: cannot get this method to work properly

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default cannot get this method to work properly

    I'm trying to take a given array and increment the capacity by a certain integer. The program runs and returns the original and incremented array with the same length. Any ideas?

    public static void main(String[] args)
    	{
    	  double[] james=new double[15];
     
    	  System.out.println("The array james has: "+james.length+" spaces");
     
    	  expand(james,5);
     
    	  System.out.println("The array now has: "+james.length+" spaces");
     
     
    	}
     
    	public static void expand(double [] x, int howManyMore)
    	{ 
    	    double [] newX=new double[x.length+howManyMore];
     
    	    for(int i=0;i<x.length;i++)
    	    {
    	     newX[i]=x[i];
     
    	    }	
    	     x=new double[newX.length]; 
     
    	    for(int i=0;i<newX.length;i++)
    	     {
    	          x[i]=newX[i];  
    	     }	 
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: cannot get this method to work properly

    It seems you are passing a value to a method and expecting it to be recreated, which is incorrect (google 'java pass by value' for much more information). Rather, return the newly created array and let the caller deal with it. The following code snipped demonstrates, and shows that recreating a parameter object within a method does nothing to the original object.
    public static void main(String[] args){
        String myValue = "myValue";
        String returned = changer(myValue);
        System.out.println("Original value: " + myValue);
        SYstem.out.println("Returned value: " + returned);
    }
    public static String changer(String value){
        value = "new value";
        return value;
    }

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: cannot get this method to work properly

    Alright, I got it to work. Thanks. I was initially confused because it was supposed to be a void method, and I was having difficulty changing the initial array into the larger one without returning anything. I appreciate your help.

Similar Threads

  1. my FileReader(i think) isn't performing properly..
    By weej25aya in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 21st, 2011, 10:06 PM
  2. Complete but not working properly
    By Noob101 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 03:17 PM
  3. Code doesn't execute properly
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 12:36 PM
  4. I cannot get boolean to work properly
    By kl2eativ in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 18th, 2011, 07:37 AM
  5. Replies: 3
    Last Post: April 18th, 2010, 10:08 AM