简体   繁体   中英

ArrayList won't print in console

I have an Employee class with one method named addEmployee which manipulates an ArrayList to add employees. My following code won't print the list on the console screen. I can't find out what's wrong with my code.

package com.sib.Tmanager;

import java.util.ArrayList;
import java.util.Scanner;

public class Employee {
    private String EmpFName;
    private String EmpLName;

    public Employee(String empFName, String empLName) {
        super();
        EmpFName = empFName;
        EmpLName = empLName;
    }
    public String getEmpFName() {
        return EmpFName;
    }
    public void setEmpFName(String empFName) {
        EmpFName = empFName;
    }
    public String getEmpLName() {
        return EmpLName;
    }
    public void setEmpLName(String empLName) {
        EmpLName = empLName;
    }

    public static void addEmployee()
    {
        ArrayList<Employee> Emplist= new ArrayList<Employee>();

        Scanner s=new Scanner(System.in) ;

        System.out.println("Enter the Firstname of the employee");
        String Fname= s.next();
        System.out.println("Enter the Lastname of the employee");
        String Lname= s.next();

        Employee emp = new Employee(Fname, Lname);
        Emplist.add(emp);

        //System.out.println(emp.EmpFName +" "+ emp.EmpLName);

        System.out.println(Emplist);
    }

}

I tried to change my code by overriding the ToString() method and I still have the same following output.

Enter employee's Firstname

jason

Enter employee's Lastname

karl

[com.sib.Tmanager.Employee@1a758cb]

This may not be an answer, but a bit of advice about class design.

  1. Think about an Employee as a physical object.
    • Should an Employee have a first name? Yes.
    • Should an Employee have a last name? Yes.
    • Should an Employee be filled with other Employyes ? Absolutley not.
  2. If the above third point doesn't make sense, what should you do?
    • Create a class called something like EmployeeList . Should an EmplpoyeeList have Employees ? Why, of course!
    • The above class is the class you want to have the Employee ArrayList
    • Also, this is where you want to have the addEmployee method, so you can add the Employee the EmployeeList

Here's a example

public class EmployeeList {
    ArrayList<Employee> employees;

    public EmployeeList() {
        employees = new ArrayList<Employee>();
    }

    public void addEmployee(Employee employee) {
        employees.add(employee);
    }

    public void printEmployees() {
        for (Employee e : employees) {
            System.out.println(e);
        }
    }
}

So in the main , you first will create an EmployeeList then add Employee s to it.

public static void main(String[] args) {
    EmployeeList list = new EmployeeList();
    list.addEmployee(new Employee("Jim", "Bo");
    list.addEmployee(new Employee("Foo", "Bar");
    list.addEmployee(new Employee("First", "Last");

    list.printEmployees();
}

Note: to get the Employee object to print out as String representation, you should override the toString() method as others have suggested.

You can use Arrays.toString(empList.toArray(new Employee[0])) or empList.toString() in order to print the contents of an ArrayList . This will print nicely if you override toString() in Employee .

call AddEmployee() from public static void main()

System.out.println(Emplist)

you are trying to print Emplist object

you have to iterate the arraylist to show it's content

You should assign the ArrayList in the class fields and initialize it in the Constructor

public class Employee {
private String EmpFName;
private String EmpLName;
private ArrayList<Employee> Emplist;
.
.
.
}

public Employee(String empFName, String empLName) {
    super();
ArrayList<Employee> Emplist= new ArrayList<Employee>();
    EmpFName = empFName;
    EmpLName = empLName;
}

Now when you need to add you only do :

this.Emplist.add(x);

in the addEmployee method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM