Jump to content

Method arguments for Array and Return


Debis

Recommended Posts

I want to create method for accepting arguments for an array. I want to declare the arguments inside main method. Then want to use the return statement in my method to display the array elements. How can I do that? Please help as I am a beginner.

Link to comment
Share on other sites

//I hope help you @debis

public class ArraysInJava
{
    public static void main(String[] args)
    {
        //declaring and creating An array object of double type containing 8 elements
 
        double[] d = new double[8];   
 
        d[2] = 10.25;   //Changing the value of element at index 2
 
        d[5] = 15.35;   //Changing the value of element at index 5
 
        methodOne(d);   //Passing array to methodOne()
 
        //Changes made to array object in the method are reflected in the actual object
 
        System.out.println(d[2]);     //Output : 25.32
 
        System.out.println(d[5]);     //Output : 45.89
    }
 
    static void methodOne(double[] d)
    {
        d[2] = 25.32;   //Changing the value of element at index 2
 
        d[5] = 45.89;   //Changing the value of element at index 5
    }
}

 

Edited by Makwana Prahlad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...