This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

create array of objects  RSS feed

 
Stephanie Ll
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Lets make as if I have a class called Employer. How do I create an array of Employer.
I've tried the codes below but it doesn't work as I get "Employer@119c082" instead of Tim

Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);

What is wrong with my codes?
 
Mike Noel
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the Employer class isn't overridding the toString() method so you are getting the default implementation which just prints out the reference.

_M_
 
Abdi Duale
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------
Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);
----------------------------------------------------

Try using a for loop i.e

for(int i =0; i<emp.length; i++)
System.out.print(" "+emp[i]);
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abdi Duale:
System.out.print(" "+emp[i]);

Nope. That produces the same output -- calling Employee's toString method,
at the expensive of a needless -> "" + <-

OP: your problem has nothing to do with arrays: you should first try to
get output from a single, object:
 
Balaji Sampath
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesnt the above code needs a constructor wih a String parameter:
public Employer(String name){
// do something
}

Regards
Balaji.S
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Balaji Sampath:
Doesnt the above code needs a constructor wih a String parameter:
public Employer(String name){
// do something
}


Yes.
 
Stuart Goss
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephanie Ll:
Hi
How do I create an array of Employer.

You already know

Originally posted by Stephanie Ll:

Employer [] emp = new Employer[2]
emp[0] =new Employer("Tim");
System.out.println(emp[0]);

What is wrong with my codes?

Nothing is wrong with this code
You've created an array of Employers ("new") and assigned it to a variable that can contain such ("emp").
Then you've created an Employer object ("new") and assigned it to an element of the array.

That's all there is to it

Stuart
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!