Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By superhaNds

Thread: Need some help with methods

  1. #1
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Unhappy Need some help with methods

    Hello,

    I have a question. My assignment is to create a method called 'fillArray' that accepts an integer array and a second integer. What this method should essentially do when invoked is, for example, if I have an array called ‘numbers’ and I invoke the method: fillArray(numbers, -3); this method would fill the array 'numbers' with the value -3 for every element of the array.

    I'm having trouble understanding what the purpose of the first argument is. I understand how to fill an array using a method but I'm not sure how to go about this method. I have typed up something to work with, I hope:

    Java Code:
    public static void main(String[] args) {
    		
    		
    		int[] numbers = new int[5];
    		System.out.println(fillArray(numbers, -3));
    
    	}
    
    public static int[] fillArray(int[] randomArray, int elementInteger)
    	{
    		//randomArray = new int[10];
    		for (int i=0; i<randomArray.length; i++)
    		{
    			randomArray[i] = elementInteger;
    		}
    		return randomArray;
    		
    	}
    I know my directions do not require me to print out anything but I want to know that it's actually working. So as I tried this code I would only be given as output the reference. (i.e. "[I@3487a5cc") There's no size to the array that was given so I just made it a small array just to see if it's doing what it's supposed to. Evidently, it's not. I know the for-loop should be filling this array just fine so that means there's something wrong with how I have created the array. I didn't know if I should place the size of the array inside the for-loop or outside the for-loop. I also don't understand the purpose of the parameter to name the array at all.

    Looking for some guidance, please.

    Thank you in advance!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,946
    Rep Power
    4

    Default Re: Need some help with methods

    Your method is working fine. Here are a couple of observations.

    1. When you print it out you are printing out the array reference and not the array contents. To print out the contents, try something like this in main.

    Java Code:
    fillArray(numbers, -3);
    for (int n : numbers) {
         System.out.println(n);
    }
    2. Since you are passing an array reference to the method, you do not have to return anything. You can just use the same array (as was demonstrated in the previous example).

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

  3. #3
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

    Default Re: Need some help with methods

    Thank you, Jim!

  4. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    236
    Rep Power
    1

    Default Re: Need some help with methods

    to display the array, you can also call Arrays.toString(your array);
    import first java.util.Arrays;
    exeye0h likes this.

  5. #5
    exeye0h's Avatar
    exeye0h is offline Member
    Join Date
    Jan 2012
    Location
    Chicago, IL, USA
    Posts
    40
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  3. Java Noob, trying to call methods from another methods
    By gabrielpr12 in forum New To Java
    Replies: 8
    Last Post: 11-17-2011, 09:07 PM
  4. Replies: 1
    Last Post: 10-23-2011, 12:37 PM
  5. Replies: 1
    Last Post: 10-05-2011, 12:15 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
  •