Results 1 to 6 of 6
  1. #1
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Question Return array data from a method back to main...

    Hey Guys,

    I'm stumped. I'm trying to return an array back to main. The array returned to main should contain the reversed random numbers array. I believe I have the array correctly reversed within the 'reverseArray' method. I'm trying to send this array back to main, but it appears to contain empty data (a bunch of zeros). Can someone point me in the right direction?

    Thanks,

    Joe

    Java Code:
    class ArrToMain {
    public static void main(String[] args) {
    	
    	final int NUMBER_OF_ELEMENTS = 1000;
    	double[] numbers = new double [NUMBER_OF_ELEMENTS];
    	
    	
    	//Invoke initialize method
    	initialize(numbers);
    	
    	//Use for loop to display contents of numbers array
    	System.out.println("The random numbers RETURNED to main:");
    	for (int i = 0; i < numbers.length; i++) {
    		System.out.print("numbers[" + i + "] ");
    		System.out.println(numbers[i] + " ");
    	} //End of print for loop
    	
    	//Invoke reverseArray method
    	reverseArray(numbers);
    	
    		System.out.println(" ");
    		System.out.println("Inside the 'main' method:");
    		System.out.println("numbers[999] = " + numbers[999]);
    	
      } //End of main method
    
    
    
    /**********************************************************************/
    //Define a method called 'initialize' to receive empty numbers array
    public static double[] initialize(double[] numbers){
    	
    	//use for loop to fill array with random numbers
    	for (int i = 0; i < numbers.length; i++) {
    		numbers[i] = Math.random()*1000;
    	} //End of for loop
    	
    	//Return array to main
    	return numbers;
    	
    	} //End of 'initialize' method
    	
    	
    /**********************************************************************/
    //Define a method called 'reverseArray' to reverse numbers of the array
    public static double[] reverseArray(double[] numbers){
    	System.out.println("");
    	System.out.println("Inside the 'reverseArray' method:");
    	System.out.println("numbers[999] = " + numbers[999]);
    	
    	double[]reverseNumbers = new double[numbers.length];
    	
    	for (int j = 0; j < numbers.length; j++) {
    		reverseNumbers[numbers.length-j-1] = numbers[j];
    	}
    	
    	System.out.println("reverseNumbers[0] = " + reverseNumbers[0]);
    	
    return reverseNumbers;
    
    	} //End of reverseArray method
    
    } //End of class ArrToMain

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Return array data from a method back to main...

    I'm trying to return an array
    The call to the method should save the value that is returned by the method in a variable.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,417
    Rep Power
    5

    Default Re: Return array data from a method back to main...

    You are not returning anything. Do it like this:

    Java Code:
    numbers =  reverseArray(numbers);
    You also invoked your initialize method incorrectly. The only reason it worked is that you passed
    it an array reference and filled in what the reference "referred" to. The return didn't do anything.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: Return array data from a method back to main...

    Thanks guys.

  5. #5
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: Return array data from a method back to main...

    Quote Originally Posted by jim829 View Post
    You are not returning anything. Do it like this:

    Java Code:
    numbers =  reverseArray(numbers);
    You also invoked your initialize method incorrectly. The only reason it worked is that you passed
    it an array reference and filled in what the reference "referred" to. The return didn't do anything.

    Regards,
    Jim
    Jim,

    I did get the code to work with your (and Norm's) direction. Thanks!

    ...but I'm not sure I understand my error in invoking the method. Did I correct this mistake when I assigned it to a variable? If not, can you point me to an example of the right and wrong way?

    Thanks,

    Joe

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,417
    Rep Power
    5

    Default Re: Return array data from a method back to main...

    Methods either return a value or they don't. If they don't return a value, then use the void return type.

    Java Code:
    public void setA(int a) {
       this.a = a; // sets the instance field a to the value of the argument
    }
    
    // If they do return a value, then the return type needs to be specified.
    
    public int getA() {
       return this.a; // returns the instance field of a
    }
    // or
    public int sum(int a, int b) {
       return a + b;  // return their sum
    }
    
    // Special situations.
    
    public void setA(int a) {
       a = 10;  // does not change the passed value of a.
    }
    
    // The following works because you are changing the contents of the passed array.
    // not the array name or reference.  Just what the array points to.
    // So the original array is over written.
    
    public void initArray(int [] m) {
       for (int j = 0; j < m.length; j++) {
          m[j] = m[j] * 10;
       }
    }
    
    // If you want to preserve the original array, do it like this.
    
    public int[] initArray(int [] m) {
       int [] n = new int[m.length];
       for (int j = 0; j < m.length; j++) {
          n[j] = m[j] * 10;
       }
       return n;
    }
    For more details on methods and other aspects of Java, check out the Oracle tutorials in my signature.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 4
    Last Post: 03-28-2013, 08:40 PM
  2. Replies: 1
    Last Post: 11-29-2012, 09:42 AM
  3. Replies: 10
    Last Post: 12-04-2011, 01:16 PM
  4. problem in a method which return array format
    By elenora in forum Advanced Java
    Replies: 13
    Last Post: 09-21-2011, 09:43 AM
  5. Replies: 2
    Last Post: 04-16-2008, 12:35 PM

Posting Permissions

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