This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Hands On Software Engineering with Python and have Brian Allbey on-line!
See this thread for details.
Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

how to return an array from a method  RSS feed

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • 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: 24203
40
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just like this:

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • 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
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
yup ... you are correct, Kelvin. i didnt notice that ... thanks
 
author
Posts: 8991
19
  • Mark post as helpful
  • send pies
  • 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
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!