简体   繁体   English

ArrayList不会在控制台中打印

[英]ArrayList won't print in console

I have an Employee class with one method named addEmployee which manipulates an ArrayList to add employees. 我有一个Employee类,其中有一个名为addEmployee方法,它操纵ArrayList来添加员工。 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. 我试图通过重写ToString()方法来改变我的代码,我仍然有相同的以下输出。

Enter employee's Firstname 输入员工的名字

jason 贾森

Enter employee's Lastname 输入员工的姓氏

karl 卡尔

[com.sib.Tmanager.Employee@1a758cb] [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. Employee视为物理对象。
    • Should an Employee have a first name? Employee应该有名字吗? Yes. 是。
    • Should an Employee have a last name? Employee应该有姓吗? Yes. 是。
    • Should an Employee be filled with other Employyes ? 如果一个Employee充满其他Employyes Absolutley not. 绝对没有。
  2. If the above third point doesn't make sense, what should you do? 如果上述第三点没有意义,你应该怎么做?
    • Create a class called something like EmployeeList . 创建一个名为EmployeeList的类。 Should an EmplpoyeeList have Employees ? EmplpoyeeList是否应该有Employees Why, of course! 为什么,当然!
    • The above class is the class you want to have the Employee ArrayList 上面的类是您想要拥有Employee ArrayList
    • Also, this is where you want to have the addEmployee method, so you can add the Employee the EmployeeList 此外,这是您想要使用addEmployee方法的地方,因此您可以添加Employee 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. 所以在main ,你首先会创建一个EmployeeList然后添加Employee s。

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. 注意: Employee对象以String表示形式打印出来,您应该像其他人建议的那样覆盖toString()方法。

You can use Arrays.toString(empList.toArray(new Employee[0])) or empList.toString() in order to print the contents of an ArrayList . 您可以使用Arrays.toString(empList.toArray(new Employee[0]))empList.toString()来打印ArrayList的内容。 This will print nicely if you override toString() in Employee . 如果在Employee覆盖toString() ,这将很好地打印。

call AddEmployee() from public static void main() 从public static void main()调用AddEmployee()

System.out.println(Emplist) 的System.out.println(Emplist)

you are trying to print Emplist object 你正在尝试打印Emplist对象

you have to iterate the arraylist to show it's content 你必须迭代arraylist来显示它的内容

You should assign the ArrayList in the class fields and initialize it in the Constructor 您应该在类字段中分配ArrayList并在构造函数中初始化它

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); this.Emplist.add(X);

in the addEmployee method 在addEmployee方法中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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