实际上我今天一直在练习一些java程序(试着自己学习)。我正在尝试编写一个程序,该程序接受来自用户的许多员工详细信息并显示它。我写了相同的代码,但失败了。那里没有编译错误。我试图创建一个ArrayList来放置从用户那里获得的细节。但不太确定如何做到这一点。或者应该通过使用对象数组来完成?
任何人都可以帮助我吗?另外请建议我改进编码的方法。我会非常感激的。
以下是我的代码:
public class EmployeeList{
String empname;
String empaddress;
int empage;
public void getEmployeeDetails(){
try{
final BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Name :");
empname = br.readLine();
System.out.println("Address :");
empaddress = br.readLine();
System.out.println("Age :");
empage = Integer.parseInt(br.readLine());
}
catch(final IOException e){
e.printStackTrace();
}
}
public void displayEmployeeDetails(){
System.out.println("Name :" + empname);
System.out.println("Address :" + empaddress);
System.out.println("Age : " + empage);
}
}
SerialisePersonList.java
public class SerialisePersonList{
public static void main(final String[] args){
final EmployeeList emp = new EmployeeList();
System.out.println("Enter 3 person details");
for(int i = 1; i <= 3; i++){
System.out.println("Enter details of person" + i);
final List details = new ArrayList();
details.add(emp.getEmployeeDetails());
}
System.out.println("The entered person Details are as following");
for(int i = 1; i <= 3; i++){
System.out.println("Details of person" + i);
emp.displayEmployeeDetails();
}
}
}
答案 0 :(得分:1)
您需要在循环范围之前创建arraylist并在循环中添加特定员工
public static void main(String[] args)
{
List<EmployeeList> details = new ArrayList<EmployeeList>();
System.out.println("Enter 3 person details");
for(int i=1;i<=3;i++)
{
System.out.println("Enter details of person"+i);
EmployeeList emp =new EmployeeList();
emp.getEmployeeDetails();
details.add(emp);
}
System.out.println("The entered person Details are as following");
for(EmployeeList employee : details)
{
System.out.println("Details of person");
employee.displayEmployeeDetails();
}
}
答案 1 :(得分:0)
EmployeeList
重命名为Employee
List
和ArrayList
通用:List<Employee>
和ArrayList<Employee>
答案 2 :(得分:0)
Class EmployeeList
但是
概念上它是Class Employee
因为它
代表一个员工,而不是一个列表
他们。getEmployeeDetails()
来
readEmployeeDetails()
更有意义。emp
和details
不应该是最终的,它们不是常量,它们是变量。我认为你对列表和属于列表的元素有点混淆。
答案 3 :(得分:0)
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Employee{
private String name;
private String address;
private int age;
public void setName(String name){
this.name = name;
}
public void setAddress(String address){
this.address = address;
}
public void setAge(int age){
this.age = age;
}
public static Employee read() throws Exception{
Employee tmp = new Employee();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Name :");
tmp.setName(br.readLine());
System.out.println("Address :");
tmp.setAddress(br.readLine());
System.out.println("Age :");
tmp.setAge(Integer.parseInt(br.readLine()));
return tmp;
}
public void display(){
System.out.println("Name :" + this.name);
System.out.println("Address :" + this.address);
System.out.println("Age : " + this.age);
}
}
其次是
import java.util.ArrayList;
public class SerialisePersonList{
public static void main(final String[] args){
ArrayList<Employee> list = new ArrayList<Employee>();
System.out.println("Enter details for 3 people");
for(int i = 1; i <= 3; i++){
System.out.println("Enter details of person " + i);
try{
list.add(Employee.read());
}catch(Exception e){
e.printStackTrace();
}
}
System.out.println("The entered person Details are as following");
for(Employee emp : list){
System.out.println("======================");
emp.display();
}
}
}
应该涵盖它