문제

I'm trying to output an arraylist in the following line of code, but I only receive something like this Employee@160eaab5

public class Company {
    static ArrayList <Employee> list1 = new ArrayList <Employee> ();
    public static void main() {
        list1.add (new Employee ("John", "Smith", 11.0));
        list1.add (new Employee ("James", "Bond", 7.0));
        list1.add (new Employee ("Fabio", "Jones", 6.9));
        list1.add (new Employee ("Simon", "Geeha", 10.0));
        output();
    }
    public static void output() {
        System.out.println("First Name\tLast Name\tSalary in thousands of dollars");
        for (int x = 0; x < list1.size(); x++) {
            System.out.println ((Employee) list1.get(x));
        }
    }
}

The class below provides the constructor and fields for the arraylist above.

public class Employee {
    public String first;
    public String last;
    public double money;

    public Employee (String s1, String s2, double s3) {
        first = s1;
        last = s2;
        money = s3;


    }

 }
도움이 되었습니까?

해결책

You need to define a toString() method for your Employee class. By defining a custom method, you can make the String representation of your object look however you like. Otherwise, The default Object#toString() implementation gets called, which looks basically like "ClassName@memoryaddress", as you've discovered.

For instance (extrapolating from your println above), if you want Employee#toString() to return the employee's information as first name, last name, and salary, separated by tabs, you could do this:

public class Employee {
    // Class members and other stuff as you have them already go here...

    @Override
    public String toString() {
        // Override the default toString() method and return a custom String instead.
        return String.format("%s\t%s\t%s", first, last, money);
    }
}

다른 팁

You need to override the default toString() method of your Employee Class.

java.util.Arrays.toString(myArrayList.toArray());

This works only as well as each element has implemented its toString(). For an ArrayList<Integer>s, for example, this would print out

[1,2,3,4,5]

So as long as your Employee object has a good toString implementation, this will work well for you.

Add a toString method for Employee class

For iterating and printing you can use the foreach loop.

for (Employee e : list1) {
 System.out.println (e);
}

Just in case you were wondering what Employee@160eaab5 corresponds to it is the unsigned hexadecimal representation of the hashcode of your Employee object.

Simply implement Employee.toString(), and then you can use list1.toString():

public class Employee {
    public String first;
    public String last;
    public double money;

    public Employee (String s1, String s2, double s3) {
        first = s1;
        last = s2;
        money = s3;
    }

    @Override
    public String toString() {
       return "first=" + first + ", last=" + last + ", money=" + money;
    }
}

and then:

System.out.println(list1);

System.out.println() simply calls toString() on your object to transform it to a String. Since you haven't overridden toString() in Employee, the default Object.toString() method is called, which displays the type of the object (Employee) frollowed by its hashCode().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top