• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to return an array from a method

 
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test
{
public int methode()
{
int a = {1,2,3,4,5};
return a; // how to return it ??? return a[] or anyother way?
}
}

give me a solution ...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like this:

 
Ranch Hand
Posts: 170
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of returning a single integer in
public int methode()
Return an array of integers like
public int[] methode()
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your problem isn't with the "return", it's with the earlier declaration of variable "a" and the declaration of the method. In both cases, you declared it as "int", but it is [a reference to] an array, so should have been declared "int[]".

Once your method and your variable "a" are declared as "int[]", you can just use "return a" to return [a reference to] the array.
 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i tried to compile the following program, i got an incompatible type error ... (for returning an array) ....

import java.lang.reflect.Array;

class ResizeArray
{
static int z;
public int resizeArr(int [] b,int n)
{

z = n;
b = new int[10];
b = (int[])ArrayUtils.expand(b);
return b;

}

public static Object expand(Object a)
{
Class cl = a.getClass();
if (!cl.isArray()) return null;
int length = Array.getLength(a);
int newLength = z;
Class componentType = a.getClass().getComponentType();
Object newArray = Array.newInstance(componentType, newLength);
System.arraycopy(a, 0, newArray, 0, length);
return newArray;
}

public static void main(String args[])
{
ResizeArray ra = new ResizeArray();
int a[] = {1,2,3,4,5};
System.out.println("main length "+a.length);
int d=3;
ra.resizeArr(a,d);
}
}

watz the error in it ???
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manisekar,

You forgot to change the return type of your resizeArr() method to declare an array return type:

public int[] resizeArr(int [] b,int n)
{
z = n;
b = new int[10];
b = (int[])ArrayUtils.expand(b);
return b;
}

 
Manisekar Chinnasami
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup ... you are correct, Kelvin. i didnt notice that ... thanks
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Manisekar,

You've hit upon a very, very important idea in Java, and in OO languages in general. It's useful to think of the methods you create for your objects as message senders and receivers. As you get deeper and deeper into the language you'll see that a method's "signature" (in other words the messages it must receive and the message it returns) are always a crucial part of understanding the method itself - in many cases the ONLY thing you'll know about how a method works is what you must send to it, and what it will return to you. So, you're on the right track figuring out how to return an array!

Bert
 
Ranch Hand
Posts: 91
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try the following code to do the same thing.


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manishekar, there is an easy way to return an array from a method in java. You can call a method by value or by reference.

one thing here to note that Java always passes arrays and vectors by reference. So, any modification to the array elements in another function would mean that the original array would also be modified similarly. However, returning arrays from a function is pretty simple. A method that returns an array needs to have the return type of the function set to the appropriate data type for the array.

Here is the code I got online from this article.


I assume that you know how to pass the array. This code will help you learn how to return an array from a method.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Mark!

Mark Nathon wrote:You can call a method by value or by reference.


No. In Java you can only call methods by value. When you pass an array to a method, you really pass a reference to the array, by value.

So, any modification to the array elements in another function would mean that the original array would also be modified similarly.


This is not what "pass by reference" means. Pass by reference means that the method parameter becomes an alias for a variable in the calling method. When you change the value of the parameter, you also change the value of the variable in the calling method directly. This is something completely different from passing array or object references by value.

The phrase "pass by reference" is meaningless for return values. A language that supports "pass by reference" only supports it for method parameters. However, Java doesn't support pass by reference at all.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again)

Mark Nathon wrote:. . . You can call a method by value or by reference. . . . Java always passes arrays and vectors by reference. . . .

I see Stephan has already explained your mistake. Thank you for giving us that link It is unfortunate that people still write that sort of rubbish in tutorials.

Please don't call what you wrote a function; call it a method. It doesn't actually represent a function because it takes no input.
 
Ranch Hand
Posts: 180
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to notice some hits, so when your write return a, you would know that you return an object reference, not the returned array, probably there were two possible to return the desired array, 1 - you tape new int[]{}{} or you use for loop and change the method to void type
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid your question isn't clear, HE.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic