0

So I have a class called Employee in which I created an ArrayListfor in another class. I am trying to print the values of the list but they print each element's object reference. I forgot how to do this, I have tried looking it up elsewhere but can't seem to find an answer.

Here is the Employee Class:

public class Employee {

int employeeID;
String employeeName;

public Employee(int employeeId, String employeeName){
this.employeeID = employeeId;
this.employeeName  = employeeName;

}
...

Here is where I print my values:

public void printArrListValues() {

   for(Employee x: employeeList){

        System.out.println(x);
    }
//    Arrays.toString(employeeNameLst);

}

I did try using .toString() on x, however this did not solve the issue.

The console printed this to me:

binarytree.Employee@78da5318
binarytree.Employee@45858aa4
binarytree.Employee@425138a4
binarytree.Employee@625db8ff
binarytree.Employee@771c9fcc
binarytree.Employee@783f472b
binarytree.Employee@25995ba
binarytree.Employee@4774e78a
BUILD SUCCESSFUL (total time: 0 seconds)
6
  • Add toString() to Employee. Commented Jan 9, 2015 at 23:28
  • why not use Vector or HashMap. Its easier to retrieve random values in the collection Commented Jan 9, 2015 at 23:38
  • 1
    Vector has been superseded by ArrayList way back in Java 1.2. Commented Jan 10, 2015 at 0:02
  • @TheUknown I decided to use a HashMap instead, I did look at various alternatives but that seems the best as I can just use a TreeMap to order my key values and I wouldn't need theEmployee class for any of it. Commented Jan 10, 2015 at 0:27
  • @ajb I know, I still use it though sometimes :) @ jp24 Glad, my suggestion helped Commented Jan 10, 2015 at 1:38

2 Answers 2

3

When you pass an object to println, eventually, toString() will be called. Because you didn't override toString(), Employee inherited Object's toString() method, which is responsible for the output you see.

In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Override toString in Employee, and return the String you want visible when the Employee is printed.

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I don't fully understand what I need to do. Do I need to go inside my Employee class and then create a toString() method that takes an Employee as an argument then print its content using System.out.print()?
Not quite right. Create a method with the signature public String toString() in Employee, no parameters. Use your instance variables employeeID and employeeName when creating a String that represents how you want the output, and return that String; don't print it. The println method will print whatever is returned by toString().
Yes override toString() and make it return meaningful information. Joshua Bloch even cites this a good practice as it makes your class much more pleasant to use, especially with IDEs and testing. Some IDEs like Eclipse will even provide tools to override it for you.
0
  1. you can override the method toString in class employee
  2. or x.getter methods

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.