Results 1 to 3 of 3
  1. #1
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Question Problem printing ArrayList

    Hi!

    I want to create an ArrayList of Objects (example User - class user) so I search for an example!
    I found it but it has a little problem. When I print this test code (I got it online) I get this output (I'm working with eclipse):

    [Employee@4e82701e, Employee@558ee9d6, Employee@199a0c7c]
    [Employee@4e82701e, Employee@558ee9d6, Employee@199a0c7c]

    This is the code:
    Java Code:
    import java.util.ArrayList;
    public class MainClass {
      public static void main(String[] a) {
        ArrayList<Employee> emps = new ArrayList<Employee>();
        emps.add(new Employee("A", "G"));
        emps.add(new Employee("T", "A"));
        emps.add(new Employee("K", "J"));
    
        System.out.println(emps);
    
        Employee e = emps.get(1);
        e.setLastName("new");
    
        System.out.println(emps);
      }
    }
    
    class Address {
    }
    
    class Employee {
      private String lastName;
    
      private String firstName;
    
      private Double salary;
    
      public Address address;
    
      public Employee(String lastName, String firstName) {
        this.lastName = lastName;
        this.firstName = firstName;
        this.address = new Address();
    
      }
    
      public String getLastName() {
        return this.lastName;
      }
    
      public void setLastName(String lastName) {
        this.lastName = lastName;
      }
    
      public String getFirstName() {
        return this.firstName;
      }
    
      public void setFirstName(String firstName) {
        this.firstName = firstName;
      }
    
      public double getSalary() {
        return this.salary;
      }
    
      public void setSalary(double salary) {
        this.salary = salary;
      }
    }
    I have already tried different aproaches but I don't understand whats happening! Probably it's nothing...but I'm just starting...
    Please help! :)

    Thanks!

    Paula
    Last edited by sunde887; 09-13-2011 at 08:23 PM. Reason: Code tag

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Problem printing ArrayList

    An ArrayList doesn't have any trouble printing itself; it just calls the toString() method for every element in the list and wraps a '[' and ']' around it. Does your Employee class have a (more or less) decent toString() method? I guess not ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    pamm is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Re: Problem printing ArrayList

    I guess your right, Jos! :)
    Thanks!

    Paula

Similar Threads

  1. Replies: 9
    Last Post: 08-19-2011, 01:59 AM
  2. About printing Arraylist on JSP page....
    By vaibhavspawar in forum Advanced Java
    Replies: 0
    Last Post: 08-13-2010, 07:52 AM
  3. ArrayList printing
    By tommyyyy in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 05:33 AM
  4. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •