Solved

return in method

Posted on 2004-08-17
22
317 Views
Last Modified: 2010-03-31
Hi experts,
when using a method that returns an array how do you assign the values of the return array to another array?

I tried doing something like:
array[] = method call;
but apperently that doesnt work :)

thank you
0
Comment
Question by:Samooramad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 5
  • +1
22 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11819702
>>but apperently that doesnt work :)

Should do. Please post code
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819760
if your method is returning a String[] then

String array[] = method call;  // this method should return String[]..
// otherwise, you have to cast to
// String array[] = (String[])method call;

If you already have an array element then
array = method call; // should the same type, if String[] array is returned then array should be String[]
0
 

Author Comment

by:Samooramad
ID: 11819761
public Result[] getAllDoctors(Result[] myArray, String d)
{
 
List list = new ArrayList();

    for (int counter=0; counter<myArray.length; counter++)
    {
     if (myArray[counter].getDocName().equals(d) )
      {
        list.add(myArray[counter]);
      }
    }//for


    return (Result[]) list.toArray(new Result[0]);
}

////////////////////////////////////////

Result restOfDoc[]= new Result[100];
String name;
 restOfDoc[]= getAllDoctors(allResults[], name);//this line has the error: not a statement at line 674, column 27
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 11819778
>.restOfDoc[]= getAllDoctors(allResults[], name);//

should be

restOfDoc[]= getAllDoctors(allResults, name);//
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819779
>> restOfDoc[]= getAllDoctors(allResults[], name);//this line has the error: not a statement at line 674, column 27

restOfDoc= getAllDoctors(allResults[], name);

This will fix the problem
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11819780
Sorry

restOfDoc = getAllDoctors(allResults, name);//
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819786
What is allResults[]? Where do you define it?
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 200 total points
ID: 11819791
even

restOfDoc= getAllDoctors(allResults[], name);

has to be changed to

restOfDoc= getAllDoctors(allResults, name);
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819795
"allResults" I mean. And you do not need the [] in the assignment.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 11819804
That will fix the problem ;-) But actually you should not create an array first, so it should be


 
String name = "Zhivago";
Result restOfDoc[]= getAllDoctors(allResults, name);
0
 

Author Comment

by:Samooramad
ID: 11819825
yes that did it ..could you guys explain why that is?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819833
It a wrong syntax, simply.
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 200 total points
ID: 11819838
you have to just pass the reference, not with the type ([])
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11819846
Yep, wrong syntax. getAllDoctors returns a reference to an array, so you needn't create one first as you do. Shouldn't it return Doctor[] ?
0
 

Author Comment

by:Samooramad
ID: 11819862
>>That will fix the problem ;-) But actually you should not create an array first, so it should be
 
String name = "Zhivago";
Result restOfDoc[]= getAllDoctors(allResults, name);

Im sorry CEHJ I dont know what you mean?
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 100 total points
ID: 11819881
You can simply do:

Result restOfDoc[];
String name;
restOfDoc= getAllDoctors(allResults, name);

Since the getAllDoctors method will create one array, no need to create the first one by doing:

Result restOfDoc[]= new Result[100];
0
 

Author Comment

by:Samooramad
ID: 11819886
>>Shouldn't it return Doctor[] ?

probably CEHJ.. but lets tackel problems one at a time :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11819893
You create an array:

>>Result restOfDoc[]= new Result[100];

then assign the result of that function to its reference. You don't need to create the array. The method should probably look more like:


public Doctor[] getAllDoctors(Doctor[] myArray, String doctorName)
{
 
List list = new ArrayList();

   for (int counter=0; counter<myArray.length; counter++)
   {
    if (myArray[counter].getDocName().equals(doctorName) )
     {
       list.add(myArray[counter]);
     }
   }//for


   return (Doctor[]) list.toArray(new Doctor[0]);
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819897
And also CEHJ is right, should you be getting back an array of type "Doctor"?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11819899
>>probably CEHJ.. but lets tackel problems one at a time :)

OK ;-)
0
 

Author Comment

by:Samooramad
ID: 11819965
ok now I understand .. thanks everyone..
0
 
LVL 35

Expert Comment

by:girionis
ID: 11820134
:)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 136
eclipse buid path vs tomcat lib path 10 49
How do I remove an object from a 3 64
junit example issue 2 59
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question