return in method

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
SamooramadAsked:
Who is Participating?
 
CEHJCommented:
Sorry

restOfDoc = getAllDoctors(allResults, name);//
0
 
CEHJCommented:
>>but apperently that doesnt work :)

Should do. Please post code
0
 
mmuruganandamCommented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
SamooramadAuthor Commented:
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
 
CEHJCommented:
>.restOfDoc[]= getAllDoctors(allResults[], name);//

should be

restOfDoc[]= getAllDoctors(allResults, name);//
0
 
mmuruganandamCommented:
>> 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
 
girionisCommented:
What is allResults[]? Where do you define it?
0
 
mmuruganandamCommented:
even

restOfDoc= getAllDoctors(allResults[], name);

has to be changed to

restOfDoc= getAllDoctors(allResults, name);
0
 
girionisCommented:
"allResults" I mean. And you do not need the [] in the assignment.
0
 
CEHJCommented:
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
 
SamooramadAuthor Commented:
yes that did it ..could you guys explain why that is?
0
 
girionisCommented:
It a wrong syntax, simply.
0
 
mmuruganandamCommented:
you have to just pass the reference, not with the type ([])
0
 
CEHJCommented:
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
 
SamooramadAuthor Commented:
>>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
 
girionisCommented:
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
 
SamooramadAuthor Commented:
>>Shouldn't it return Doctor[] ?

probably CEHJ.. but lets tackel problems one at a time :)
0
 
CEHJCommented:
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
 
girionisCommented:
And also CEHJ is right, should you be getting back an array of type "Doctor"?
0
 
CEHJCommented:
>>probably CEHJ.. but lets tackel problems one at a time :)

OK ;-)
0
 
SamooramadAuthor Commented:
ok now I understand .. thanks everyone..
0
 
girionisCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.