10

员工等级:

public class Employee {

    int empid;
    String name;
    int age;

    public Employee(int empid,String name,int age)
    {
        this.empid=empid;
        this.name=name;
        this.age=age;
    }
    public int getEmpid() {
        return empid;
    }
    public void setEmpid(int empid) {
        this.empid = empid;
    }
    public String getname() {
        return name;
    }
    public void setname(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    } 
}

比较器类:

public class Employee_comparator implements Comparator<Employee> {

    @Override
    public int compare(Employee object1, Employee object2) {
        return object1.getname().compareTo(object2.getname());
    }
}

主类:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Employee_Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        List<Employee> list=new ArrayList<Employee>();

        list.add(new Employee(33186,"varun",23));
        list.add(new Employee(33187,"deepak",23));
        list.add(new Employee(33188,"apple",23));
        list.add(new Employee(33189,"rohan",23));

        Collections.sort(list,new Employee_comparator());

        for(int i=0;i<list.size();i++) {

            System.out.print("age:"+list.get(i).getAge());
            System.out.print("empid:"+list.get(i).getEmpid());
            System.out.println("name:"+list.get(i).getname());
        }

        Iterator<Employee> itr=list.iterator();

        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}

当我尝试使用 get 方法打印时,它工作正常。但是,当我尝试使用 打印列表的元素时Iterator,它会给出以下输出:

new_.Employee@28122812new_.Employee@280d280dnew_.Employee@28172817new_.Employee@28082808
4

7 回答 7

10

You can implement a method toString() in the class Employee:

public String toString(){
    return "Name: "+ this.name
       + ". Age:"+ this.age
       + ". Id:"+ this.empId; 
} //example

This way, when you do:

System.out.println(itr.next());

it will print the name, the age and the id of the employee

于 2012-11-01T10:06:44.853 回答
5

Because itr.next() has return Object of the class Employee that you have sotre in it. try

    Iterator<Employee> itr=list.iterator();

    while(itr.hasNext())
    {
        Employee employee = itr.next();

        System.out.print("age:"+employee.getAge());
        System.out.print("empid:"+employee.getEmpid());
        System.out.println("name:"+employee.getname());
    }
于 2012-11-01T10:07:43.820 回答
4

您正在打印 Employee 类对象而不是它的值,

尝试 :

Iterator<Employee> itr=list.iterator();

while(itr.hasNext())
{
    //System.out.println(itr.next());
    System.out.println(itr.next().getEmpid());
}

之间,使用增强的for循环,比迭代器快。

编辑:

增强的 for 循环比 Iterator 更具可读性。而 Iterator 为您提供了使用 iterator.remove() 方法删除值的选项。如果您尝试在循环期间更新列表,for 循环将引发 ConcurrentModificationException。

于 2012-11-01T10:04:46.323 回答
2

Override the toString method so that you can get the Employee details even if its toString method is called.

public void toString(){
 return "Employee Id: "+empid+" Name: "+name+" Age: "+age;
}

Now when you call itr.next() your Employee class toString method is called and now it will return the String that you have created in the overriden toString method.

于 2012-11-01T10:07:31.370 回答
2

您可以使用以下代码在 Java 8 中执行相同操作

Iterator<Employee> employees = list.iterator(); employees.forEachRemaining(System.out::println);

或者简单地说,

list.iterator.forEachRemaining(System.out::println);

于 2018-04-22T06:48:08.277 回答
1

your itr.next() contains employee object and it will print toString() method. Since you have not override to String method you get this. You could override to String method as you want and

System.out.println(itr.next());

will print what is it in toString() method.

This will be in your Employee class

 @Override
public String toString() {
    return "Employee [empid=" + empid + ", name=" + name + ", age=" + age + "]";
}
于 2012-11-01T10:08:37.273 回答
0

你应该在你的类中添加一个 Employee 方法:

public void printEmploee()
{
  System.out.printf("ID:%s \nName: %s \nAge: %s \n",
  this.id, this.name, this.age);
}

然后使用迭代器:

Iterator<Employee> itr = list.iterator();
while (itr.hasNext()) {
itr.next().printEmploee();
}
于 2021-02-12T01:29:25.667 回答