Changing parameters in LinkedList

I am new to java, so I have some "annoying" problems. I have an Employee class that contains int idNumber

and int phone number

. Then I have it LinkedList<Employee>

, sorted by idNumber

. I want to change a specific call number idNumber

. I work with iterators, but 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 = it.next();
                if(employee.getIdNumber()== idnumber){
                    employeeTemp.setNewPhoneNumber(newphone);
                    found = true; 
                }
        }  
}

      

Yes, I know what employee.setNewPhoneNumber

is wrong, but I don't know what is the correct way to change the value in a linked list. (Sorry for the bad english, not native)

+1


a source to share


5 answers


Iterators are pain; foreach is much nicer:

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

      



I don't understand what IndexC is and I don't use LinkedList often - there might be some subtlety here, but I think you'd be better off avoiding iterators.

+2


a source


  • You are not "changing parameters in a linked list", you are trying to find an object in the list and change a property of that object
  • You have to use a map (like HashMap) instead of a list, then you don't have to iterate.
  • If you do iterate, use a for loop: for(IndexC employeeTemp: employeeTemp){}

  • Changing the phone number is usually done with a method setPhoneNubmer()

    , but it is entirely up to the class IndexC

    if it has such a method. Take a look at this class definition.
  • Always include error messages when asking for a question! "It doesn't work" is really useless information.


+1


a source


One of the reasons it doesn't work is because there is no Index in the list that satisfies (employee.getIdNumber () == idnumber).

Maybe you should put some additional code like where this list is created, did you fill it with something?

Also, that doesn't work? Setting a new phone number or retrieving an item from the list?

In both cases, I think you should post both methods, i.e.

getIdNumber();

      

As Mike B. says, perhaps using a map implementation would be better. Since you are considering ordering, perhaps a SortedMap implementation (like TreeMap) could be better.

In any case, you must override two methods in your IndexC (when using maps). Otherwise, everything will be messy.

  • is equal
  • hash code
0


a source


my bad, IndexC is Employee class, "bad copy passed" sorry. I don't like LinkedList, but I have to use it with +5000 records (School Exercise). I don't think it's recommended to use it for use with so many records. Class as set, get's, clones ..

class Manager{
private LinkedList<Employee> listE = new LinkedList<Emploee>;

public void setNewPhoneNumber(int idnumber, int newphone)
}

      

0


a source


http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html

You want to use a for loop with int increments until you find the object you want. Then you want to use listEmployee.get () to get the object you want and edit it.

However, if you need random access to such elements, then you shouldn't use Linkedlists. Insert it into ArrayList. This greatly improves random access times.

As a side note, you don't even need a for loop if the ID numbers are in order from 0-whatever. You can just listEmployee.get (idNumber)

-1


a source







All Articles