I'm trying to use a list iterator to walk a linked list and do some operations / checks on the next node depending on the integer value stored there, but I'm getting some errors in my code. I think I'm not understanding what iterator.next() is returning (some E object, but I don't know how to access the value I want from it) The editor wants me to do some casting as explained below. It gets rid of errors, but I don't know if this is a safe way to handle the problem or if it has the behavior I'm looking for. Please explain why I am getting errors and if there is a good way to handle this.
LinkedList<Integer>[] hash = new LinkedList[list.size()];
hash = remo.createHash(hash, list.size());
ListIterator iterator = list.listIterator(0);
// use the value of the integer stored at the next Node as its hash
// and add the same value to the linked list at that bucket
int i = 0;
while(iterator.hasNext()){
hash[iterator.next()].add(iterator.next());
i++;
}
// reset iterator to beginning of list
iterator = list.listIterator(0);
// if the hash bucket corresponding to the value at that node has more than
// one item in its list, remove that node from the list.
while(iterator.hasNext()){
if(hash[iterator.next()].size()>1){
iterator.remove(iterator.next());
}
}
createHash initializes each linked list in the array and remo is an instance of my class.
the editor wants me to cast iterator.next() to an int hash[iterator.next()] and it wants me to cast it to an in .add(iterator.next()).
Example: hash[(int)iterator.next()] hash[(int)iterator.next()].add((Integer)iterator.next());
LinkedList<Integer>[] hash = new LinkedList[list.size()];this line here is problematic due to docs.oracle.com/javase/tutorial/java/generics/… actually I'll make an answer out of this in a sec.