• Register
1 vote
26 views

Problem:

Hello Kodlogs,

I find this website very helpful besides the StackOverflow. You guys are really awesome and friendly. I want to know from you guys, how to pass an array to a method? Also what happens when an array is passed to a method?

Thanks for your valuable and patient answer.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

2 votes
 
Best answer

Solution:

Thanks for your compliment mate. We are trying to build a strong community with a lot of nice people like you. Let me come to the point. Java is a strongly pass-by-value programming language.

To understand this please check the code below carefully.

import java.util.Scanner;
public class MyMethod {
   public int maximum(int [] array) {
      int maximum = 0;

      for(int n=0; n<array.length; n++ ) {
         if(array[n]>maximum) {
            maximum = array[x];
         }
      }
      return maximum;
   }

   public int minimum(int [] array) {
      int minimum = array[0];
   
      for(int n = 0; n<array.length; n++ ) {
         if(array[n]<minimum) {
            minimum = array[n];
         }
      }
      return minimum;
   }

   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the size of the array:");
      int size = sc.nextInt();
      int[] myArray = new int[size];
      System.out.println("Enter the elements of the array :");
      for(int n=0; n<size; n++) {
         myArray[i] = sc.nextInt();
      }
      MyMethod x = new MyMethod();
      System.out.println("Maximum value in the array is::"+x.maximum(myArray));
      System.out.println("Minimum value in the array is::"+x.minimum(myArray));
   }
}

After compiling the program if you hit the run button this program will produce something like this:

Enter the size of the array that is to be created:
5
Enter the elements of the array:
45
12
48
53
55
Maximum value in the array is :55
Minimum value in the array is :12

As you can see I prompt the array size and it values and passes the array to my method MyMethod() and it’s returning the value of my arrays with the maximum and minimum value of the array.

13 9 6
94,240 points

Related questions

0 votes
1 answer 12 views
12 views
Problem: The keyword break in Java can be used for breaking out of a loop or switch statement. Is there anything which can be used to break from a method?
asked Feb 5 Ali 5.4k points
0 votes
1 answer 2 views
2 views
Problem: How do you create an instance method in Java? What does an instance method of a class represent? What is another name for an instance method? What is the difference between instance method and class method in Java? Can anyone have ideas to explain in detail?
asked 2 days ago Ali 5.4k points
0 votes
1 answer 45 views
45 views
I want to know how to sort an array in java without using sort method.
asked Sep 28, 2020 Daniel Anderson 4k points
3 votes
2 answers 5.5K views
5.5K views
Problem: I am struggling with a problem for a few hours back. I know how to sort an array by using methods. I was looking for another way (perhaps a loop) to do it more efficiently. Let me put my question this way, how to sort an array in java without using sort method? Could anybody here please help to make this happen? Thanks in advance
asked Mar 24, 2020 Gavin 15.3k points
0 votes
1 answer 6 views
6 views
Problem: hi i am a beginner and i want explain to me what mean this with example
asked Feb 9 ahmedadel9090 2.6k points
2 votes
1 answer 77 views
77 views
Problem: Learning a new programming language is really fun. So I am doing now. Yeah, java is fun but I stuck somewhere in static reference to the non-static method. What is this? I couldn&rsquo;t figure it out properly. I have written a program below for ... public void mySecondMethod(){ } } I just tried to call one of my functions inside another function. Any solution is much appreciated. Thanks
asked Mar 25, 2020 Gavin 15.3k points
0 votes
1 answer 46 views
46 views
Problem: Hello, I am learning functions with parameters in c++ and want to know how can I pass an array to a function in c++?
asked Oct 14, 2020 Code Learner 6.2k points
0 votes
1 answer 17 views
17 views
Problem: This is the instruction in one of the exercises in our Java class. Before anything else, I would like to say that I 'do my homework' and I'm not just being lazy asking someone on Stack Overflow to answer this for me. This specific item has been my problem out of all the other exercises because I've been struggling to find the 'perfect algorithm' for this.
asked Feb 3 Ali 5.4k points
0 votes
1 answer 44 views
44 views
Problem: Hello! I am working with ArrayLists in Java. I have declared an ArrayList and added elements in it but now I want to add another element at a specific index while moving the other elements one index ahead. I do not know how I can do that. What method can I use? Is there any method in ArrayList class that can be used for this purpose or do I have to write a custom method?
asked Nov 30, 2020 Code Learner 6.2k points
1 vote
2 answers 37 views
37 views
Problem: Hello java programmers, Myself is a new student in java. I am practicing basic java methods and objects. Finding beginner-level problems and trying to solve them. I recently got a line that says, all local variables in a method have default values. What does it mean? Can you please write a simple code that makes me understand this well? Thanks guys.
asked Mar 26, 2020 Gavin 15.3k points