• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to return array of objects in main program

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Vid Srini
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ernest,

The requirement is I need to return only the values of API and not the objects.
 
reply
    Bookmark Topic Watch Topic
  • New Topic