This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes returning an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "returning an array" Watch "returning an array" New topic
Author

returning an array

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
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

Joined: Dec 20, 2000
Posts: 49
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;
}
}
 
Don't get me started about those stupid light bulbs.
 
subject: returning an array
 
Similar Threads
Class cast question
Shallow copy of array
Updated MulitThreads Problem
How to return an array and display them?
Garbase Collection Help