This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error trying to print array to screen through toString()..

 
Waria Ahmed
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a person object and added to array through this code..

Person person1 = new Person ("Waria", 6, true);
Greggs.addPerson(person1);


Declared the array to store the person in a seperate class through...

Person employee [] = new Person [50];

And now i am trying to print the users to the screen through the following code:

public void viewMembers()
{
System.out.println(employee.toString());
}


But all i get is "[LPerson;@10b30a7" as the output. Can someone please shed some light on what the problem could be?

Sorry i am new to coding, so i probably didnt make any sense, i'd be happy to explain further if someone didnt understand?

Thanks in advance...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you call toString() on an array reference, it gives a String representation of the array object itself -- not the contents of the array.

The implementation of toString() is inherited from java.lang.Object, so you can check the API to see why you're getting things like "[LPerson;@10b30a7."

To get the array's contents, you need to iterate through the array and call toString() on each element in the array.
 
Norm Radder
Bartender
Posts: 1361
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to use the toString method, you'll also want to write/override the toString() method for your Person class to format the object's contents in some way. Otherwise you'll get something like you got for the employee array.
[ July 28, 2008: Message edited by: Norm Radder ]
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by marc weber:
To get the array's contents, you need to iterate through the array and call toString() on each element in the array.

Or call java.util.Arrays.toString(employee);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic