File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning an array

 
Bala Krishniah
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an array locally inside a method, and I want to return this array.
Interface Container{}
class Array implements Container
{
}
class MyClass
{
public static final void main(String args[])
{
MyClass myCls = new MyClass();
Array myArray = new Array[3];
myArray = myCls.method();
}
container[] method()
{
Array arr[] = new Array[3];
arr[0]= new Array("ObjectSpace0");
arr[1]= new Array("ObjectSpace1");
arr[2]= new Array("ObjectSpace2");

return arr;
}
}
// Is the method declaration correct, I could have written Array type as return type, but the requirement is to return the base type.
Please correct me if I am wrong.
 
Zheng Huang
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this.
interface Container{

}
class Array implements Container
{String s;
Array(String ps) {
s = ps;
}
public String toString() {
return s;
}
}
class MyClass
{
public static final void main(String args[])
{
MyClass myCls = new MyClass();
Array myArray[] = new Array[3];
myArray = (Array[])myCls.method();

System.out.println(myArray[0]);
System.out.println(myArray[1]);
System.out.println(myArray[2]);
}
Container[] method()
{
Array arr[] = new Array[3];
arr[0]= new Array("ObjectSpace0");
arr[1]= new Array("ObjectSpace1");
arr[2]= new Array("ObjectSpace2");
return arr;
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic