File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to return array of objects  in main program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to return array of objects  in main program" Watch "How to return array of objects  in main program" New topic
Author

How to return array of objects in main program

Vid Srini
Ranch Hand

Joined: Aug 17, 2006
Posts: 35
Hi All,

Help me out to find a solution to the below program.

1) I have declared an array called student [] in main program
2)I initialised the array inside the function void modifyRef().I can print the values inside the function.I need to get the values returned in the main function.
3)But the constraint is I should not use return type in the function.
4)I need only the values and not the objects.

Any inputs for this problem

class TestRefVar {

public static void main(String [] args){

Student st[] = null;
TestRefVar tr = new TestRefVar();
tr.modifyRef(st);
for(int i=0;i<5;i++)
System.out.println("second Values:"+ st.geti()+ " "+st.getj());
}

void modifyRef(Student [] student){
student = new Student[5];
for(int i=0;i<5;i++)
student[i] = new Student(i+1,i+2);
for(int i=0;i<5;i++)
System.out.println("third Values:"+ student[i].geti()+ " "+student[i].getj());

}
}

class Student
{
private int i;
private int j;
int score;
String name;
Student (String st, int i)
{
score =i;
name = st;
}
Student (int ii,int jj)
{
this.i = ii;
this.j = jj;
}
String getName()
{
return name;
}
int getScore()
{
return score;
}
void setScore(int i)
{
score = i;
}
public int geti(){
return i; }
public int getj(){
return j; }
}


Thanks a lot in advance,
Vidhya
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3506
    
  14
Change your code to create the array in your main method rather than in the modifyRef() method. You then just populate it in the modifyRef() method.


Joanne
Vid Srini
Ranch Hand

Joined: Aug 17, 2006
Posts: 35
Hi Joanne,

That 2 line code will give error.I replaced that with

for(int i=0;i<5;i++)
System.out.println("second Values:"+ st[i].geti()+ " "+st[i].getj());


But Iam getting null pointer exception.Since i initialised the array as null in the class TestRefVar and declared the array inside the function.

Is there anyother way to

1) initialise the array inside the function and
2) print the values outside the function.

Thanks a lot for inputs,
vidhya
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

If you must create the array object inside the method, and you must return void, then one choice is for the method to store a reference to the array someplace where the caller can get it, like in a member variable of the class. Another idea is to use the Java equivalent of "pass by reference:" make the argument to the method be an array of arrays of Student, and pass in a one-element-long, empty Student[][] as an argument. The method can then store the array in the single element of this array, and the caller will be able to retrieve it from there.

These are both really terrible designs, though; can I ask why you have these restrictions in the first place?


[Jess in Action][AskingGoodQuestions]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3506
    
  14
Just so people don't get confused, I posted an answer saying that the values were already in main(), before I noticed that the array was being initialised in modifyRef().
I deleted that, but I guess Vid had already read it.

Anyway, Vid, the answer to your new question is as posted in my first reply.
1. Create the array in main()
2. Populate it in modifyRef()
3. Print it out in main().
Vid Srini
Ranch Hand

Joined: Aug 17, 2006
Posts: 35
hi Ernest,

The requirement is I need to return only the values of API and not the objects.
 
Don't get me started about those stupid light bulbs.
 
subject: How to return array of objects in main program