Java – Changing parameters in a LinkedList


I'm new to java so im having some "annoying" problems. I have a class Employee which contains an int idNumber and a int phone number. Then I have a LinkedList<Employee> sorted by idNumber. I want to change the phonenumber of a certain idnumber.
I've been working with Iterators but i don't know if i'm doing it right, which I doubt.

public void setNewPhoneNumber(int idnumber, int newphone){
        Iterator<IndexC> it = listEmployee.iterator();   
        IndexC employeeTemp = null;

        boolean found = false;
        while(it.hasNext() && !found){ 
                employeeTemp =;
                if(employee.getIdNumber()== idnumber){
                    found = true; 

Yeah, I know employee.setNewPhoneNumber is wrong, but I don't know which the correct way change the value on the linkedlist. (Sorry for the bad english, not a native speaker)

Best Solution

Iterators are a pain; the foreach construct is a lot nicer:

public void setNewPhoneNumber(int idnumber, int newphone) {
        for (Employee employee : listEmployee)
                if (employee.getIdNumber() == idnumber) {

I'm not clear on what IndexC is, and I don't often use LinkedList - there could be some subtlety here I'm missing - but I think you're better off avoiding the iterators.

Related Question