Returning Arrays in Java [closed]

I have absolutely no idea as to why this code won't return an array... I feel like there is a problem with my compiler:

public class trial1{ public static void main(String[] args){ numbers(); } public static int[] numbers(){ int[] A = {1,2,3}; return A; } } 

The code returns nothing at all. It's driving me crazy!

7

6 Answers

It is returning the array, but all returning something (including an Array) does is just what it sounds like: returns the value. In your case, you are getting the value of numbers(), which happens to be an array (it could be anything and you would still have this issue), and just letting it sit there.

When a function returns anything, it is essentially replacing the line in which it is called (in your case: numbers();) with the return value. So, what your main method is really executing is essentially the following:

public static void main(String[] args) { {1,2,3}; } 

Which, of course, will appear to do nothing. If you wanted to do something with the return value, you could do something like this:

public static void main(String[] args){     int[] result = numbers(); for (int i=0; i<result.length; i++) { System.out.print(result[i]+" "); } } 
9

Of course the method numbers() returns an array, it's just that you're doing nothing with it. Try this in main():

int[] array = numbers(); // obtain the array System.out.println(Arrays.toString(array)); // now print it 

That will show the array in the console.

3

You have a couple of basic misconceptions about Java:

I want it to return the array without having to explicitly tell the console to print.

1) Java does not work that way. Nothing ever gets printed implicitly. (Java does not support an interactive interpreter with a "repl" loop ... like Python, Ruby, etc.)

2) The "main" doesn't "return" anything. The method signature is:

 public static void main(String[] args) 

and the void means "no value is returned". (And, sorry, no you can't replace the void with something else. If you do then the java command won't recognize the "main" method.)

3) If (hypothetically) you did want your "main" method to return something, and you altered the declaration to allow that, then you still would need to use a return statement to tell it what value to return. Unlike some language, Java does not treat the value of the last statement of a method as the return value for the method. You have to use a return statement ...

As Luiggi mentioned you need to change your main to:

import java.util.Arrays; public class trial1{ public static void main(String[] args){ int[] A = numbers(); System.out.println(Arrays.toString(A)); //Might require import of util.Arrays } public static int[] numbers(){ int[] A = {1,2,3}; return A; } } 
4

If you want to use the numbers method, you need an int array to store the returned value.

public static void main(String[] args){ int[] someNumbers = numbers(); //do whatever you want with them... System.out.println(Arrays.toString(someNumbers)); } 

You need to do something with the return value...

import java.util.Arrays; public class trial1{ public static void main(String[] args){ int[] B = numbers(); System.out.println(Arrays.toString(B)); } public static int[] numbers(){ int[] A = {1,2,3}; return A; } } 

You Might Also Like