控制台不打印任何内容

toiithl6  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(315)

它没有打印任何东西,没有错误。请帮助我解决他的错误。我尝试了三种打印方法,但仍然没有一种是有效的,没有任何错误。

import java.util.ArrayList;

public class JavaEight {

    public static void main(String args[])
    {
        ArrayList<Employee> Emp = new ArrayList<>();
        Employee e1 = new Employee(101, "Ravi", "Delhi", "2000");
        Employee e2 = new Employee(102, "Vineet", "Mangalore", "5000");
        Employee e3 = new Employee(103, "Punit", "Mumbai", "3000");
        Employee e4 = new Employee(104, "Shruti", "Banglore", "6000");
        Employee e5 = new Employee(105, "Ritu", "Hyderabad", "8000");

        for(int i=0; i<Emp.size();i++)
        {   
            Employee e = (Employee)Emp.get(i);
            System.out.println(e);

        }
        Emp.forEach(i -> System.out.println(Emp));

        for(Employee i : Emp)
        {
            System.out.println(Emp);
        }

    }
    }
axr492tv

axr492tv1#

这是因为你的arraylist emp是空的。您已创建employee对象,但尚未将其添加到emp。像emp.add(e1)一样添加到emp中。

eit6fx6z

eit6fx6z2#

在arraylist中添加员工。

ArrayList<Employee> Emp = new ArrayList<>();
        Employee e1 = new Employee(101, "Ravi", "Delhi", "2000");
        Employee e2 = new Employee(102, "Vineet", "Mangalore", "5000");
        Employee e3 = new Employee(103, "Punit", "Mumbai", "3000");
        Employee e4 = new Employee(104, "Shruti", "Banglore", "6000");
        Employee e5 = new Employee(105, "Ritu", "Hyderabad", "8000");
        Emp.add(e1);
        Emp.add(e2);
        Emp.add(e3);
        Emp.add(e4);
        Emp.add(e5);

希望它能解决你的问题。

csbfibhn

csbfibhn3#

emp.add(e1);etc丢失
您已经创建了employee对象,但没有将它们添加到列表中。因此列表是空的,不打印任何内容

相关问题