初学者,关于list
菜鸟,关于list
package my_project;
import java.util.ArrayList;
class employee {
private String name;
private float salary;
private String idno;
public Object getIdno;
public employee(String name, float salary, String idno) {
this.name = name;
this.salary = salary;
this.idno = idno;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSalary(float salary) {
this.salary = salary;
}
public float getSalary() {
return salary;
}
public void setIdno(String idno) {
this.idno = idno;
}
public String getIdno() {
return idno;
}
}
class employer {
// ArrayList al = new ArrayList();
ArrayList<employee> al = null;
public employer() {
al = new ArrayList<employee>();
}
public void addemp(employee emp) {
al.add(emp);
}
public void show_info(String idno) {
for (int i = 0; i < al.size(); i++) {
employee employee = (employee) al.get(i);
if (employee.getIdno.equals(idno)) {
System.out.println("该员工的信息是:" + employee.getName() + " "
+ employee.getIdno() + " " + employee.getSalary());
}
}
}
}
public class Array_List {
public static void main(String[] args) {
employee emp1 = new employee("maozedong", 1000, "SB001");
employer eeeee = new employer();
eeeee.al.add(emp1);
eeeee.show_info("SB001");
System.out.println("第1个人的信息:" + emp1.getName() + " ;" + emp1.getIdno()
+ " ;" + emp1.getSalary());
}
}
/**
上面的代码报错了,请问各位牛人,怎么改啊?
显示出来他的信息!谢谢了!
*/
------解决方案--------------------
首先提醒楼主,java命名规范要求类的首字母大写,你这代码还不符合规范
代码里面的主要问题就是Employee的getIdno这个应该是调用方法,代码如下
package my_project;
import java.util.ArrayList;
class employee {
private String name;
private float salary;
private String idno;
public Object getIdno;
public employee(String name, float salary, String idno) {
this.name = name;
this.salary = salary;
this.idno = idno;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setSalary(float salary) {
this.salary = salary;
}
public float getSalary() {
return salary;
}
public void setIdno(String idno) {
this.idno = idno;
}
public String getIdno() {
return idno;
}
}
class employer {
// ArrayList al = new ArrayList();
ArrayList<employee> al = null;
public employer() {
al = new ArrayList<employee>();
}
public void addemp(employee emp) {
al.add(emp);
}
public void show_info(String idno) {
for (int i = 0; i < al.size(); i++) {
employee employee = (employee) al.get(i);
if (employee.getIdno.equals(idno)) {
System.out.println("该员工的信息是:" + employee.getName() + " "
+ employee.getIdno() + " " + employee.getSalary());
}
}
}
}
public class Array_List {
public static void main(String[] args) {
employee emp1 = new employee("maozedong", 1000, "SB001");
employer eeeee = new employer();
eeeee.al.add(emp1);
eeeee.show_info("SB001");
System.out.println("第1个人的信息:" + emp1.getName() + " ;" + emp1.getIdno()
+ " ;" + emp1.getSalary());
}
}
/**
上面的代码报错了,请问各位牛人,怎么改啊?
显示出来他的信息!谢谢了!
*/
------解决方案--------------------
首先提醒楼主,java命名规范要求类的首字母大写,你这代码还不符合规范
代码里面的主要问题就是Employee的getIdno这个应该是调用方法,代码如下
- Java code
import java.util.ArrayList; class Employee { private String name; private float salary; private String idno; // public Object getIdno; public Employee(String name, float salary, String idno) { this.name = name; this.salary = salary; this.idno = idno; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setSalary(float salary) { this.salary = salary; } public float getSalary() { return salary; } public void setIdno(String idno) { this.idno = idno; } public String getIdno() { return idno; } } class Employer { // ArrayList al = new ArrayList(); ArrayList<Employee> al = null; public Employer() { al = new ArrayList<Employee>(); } public void addemp(Employee emp) { al.add(emp); } public void show_info(String idno) { for (int i = 0; i < al.size(); i++) { Employee employee = al.get(i); if (employee.getIdno().equals(idno)) { //这里进行了修改 System.out.println("该员工的信息是:" + employee.getName() + " " + employee.getIdno() + " " + employee.getSalary()); } } } } public class Array_List { public static void main(String[] args) { Employee emp1 = new Employee("maozedong", 1000, "SB001"); Employer eeeee = new Employer(); eeeee.al.add(emp1); eeeee.show_info("SB001"); System.out.println("第1个人的信息:" + emp1.getName() + " ;" + emp1.getIdno() + " ;" + emp1.getSalary()); } }