user137717
user137717

Reputation: 2165

Getting list values using Iterator Java

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());

Upvotes: 0

Views: 1809

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

LinkedList<Integer>[] hash = new LinkedList[list.size()];

This line is problematic due to http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html#createArrays

You cannot create arrays of parameterized types. For example, the following code does not compile:

List<Integer>[] arrayOfLists = new List<Integer>[2];  // compile-time error

Because:

Object[] stringLists = new List<String>[];  // compiler error, but pretend it's allowed
stringLists[0] = new ArrayList<String>();   // OK
stringLists[1] = new ArrayList<Integer>();  // An ArrayStoreException should be thrown,
                                            // but the runtime can't detect it.
If arrays of parameterized lists were allowed, the previous code would fail to throw the desired ArrayStoreException.

As such, you are creating an array of lists that aren't using generics (as you cannot create arrays of parameterized types), and as such, it stores Objects (it doesn't know what type you're actually planning to store). You should probably use an ArrayList instead of an Array to fix this problem, like so:

List<List<Integer>> listOfLists = new ArrayList<List<Integer>>(list.size());

//example usage
listOfLists.add(new LinkedList<Integer>()); 
for(List<Integer> currList : listOfLists)
{
     ...
}

Upvotes: 3

Bohemian
Bohemian

Reputation: 424973

Arrays and generics don't mix. Just use a List<List<Integer>>:

List<List<Integer>> hash = new LinkedList<List<Integer>>(list.size());

Upvotes: 1

Related Questions