java用arraylist写一个存员工的信息小程序
存储多个员工信息(工号姓名年龄入职时间),逐条打印所有员工姓名并输出员工个数姓名年龄什么的都存进去但是只取员工姓名...
存储多个员工信息(工号姓名年龄入职时间),逐条打印所有员工姓名并输出员工个数
姓名年龄什么的都存进去 但是只取员工姓名 展开
姓名年龄什么的都存进去 但是只取员工姓名 展开
1个回答
展开全部
public class PersonTest {
private static List<Person> list = new ArrayList<Person>();
private static void initPerson(){
list.add(new Person("张三","男",27));
list.add(new Person("李四","男",28));
list.add(new Person("王五","男",24));
list.add(new Person("赵大","女",26));
list.add(new Person("周七","男",24));
list.add(new Person("何六","女",25));
}
public static void main(String[] args) {
initPerson();
System.out.println("员工人数为:" + list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
}
}
class Person{
private String name;
private String sex;
private int age;
public Person() {
super();
}
public Person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
private static List<Person> list = new ArrayList<Person>();
private static void initPerson(){
list.add(new Person("张三","男",27));
list.add(new Person("李四","男",28));
list.add(new Person("王五","男",24));
list.add(new Person("赵大","女",26));
list.add(new Person("周七","男",24));
list.add(new Person("何六","女",25));
}
public static void main(String[] args) {
initPerson();
System.out.println("员工人数为:" + list.size());
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
}
}
class Person{
private String name;
private String sex;
private int age;
public Person() {
super();
}
public Person(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询