• Register
1 vote
29 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,260 points

Related questions

0 votes
1 answer 26 views
26 views
Problem: Hi there! I am a beginner. I saw this program on Internet: class test { private int variable; public test() { variable = 9; System.out.println("Value of variable is " + variable); } } public class Main { public static void main(String[] args) ... following output: Hello World Value of variable is 9 I am unable to understand the output of this program. Can anybody please explain it for me?
asked Mar 7 Code Learner 9.8k points
0 votes
1 answer 14 views
14 views
Problem: hi i am a beginner and i want explain to me what mean this with example
asked Feb 9 ahmedadel9090 3.1k points
0 votes
1 answer 10 views
10 views
Problem: Hi there! I want the answer to following MCQ: Which of the following is not part of a method call? method name return type parentheses all of the above are part of a method call Thanks!
asked Mar 6 Code Learner 9.8k points
2 votes
1 answer 93 views
93 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 106 views
106 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 9.8k points
0 votes
1 answer 6 views
6 views
Problem: I would like to pass a string array as a parameter to a function. Please look at the code below String[] stringArray = {'a', 'b', 'c', 'd', 'e'}; functionFoo(stringArray); Instead of: functionFoo('a', 'b', 'c', 'd', 'e'); but if I do this ... stating that convert String[] into String. I would like to know if it is possible to pass the values like that or what is the correct way to do it.
asked Apr 18 munim01 21k points
1 vote
2 answers 45 views
45 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
0 votes
1 answer 39 views
39 views
I want to make a static reference to the non-static method.
asked Sep 22, 2020 Daniel Anderson 4k points
1 vote
2 answers 131 views
131 views
Problem: Hello Kodlogs, I have a simple java code that I wrote a few minutes back I was trying to compare numbers using methods. The program throws the error again and again: this method must return a result of type int. I couldn&rsquo;t figure out the problem. Could you please shed some light on this ... []) { int x = 8; int y = 9; int z = 20; System.out.println(largest(x, y, z)); } } Many thanks
asked Mar 27, 2020 Gavin 15.3k points
0 votes
1 answer 45 views
45 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 6 Wafa Abu Yousef 6.1k points