0

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

2
  • 1
    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. Commented Aug 18, 2014 at 8:30
  • Yea I realized when I looked again. I'm trying to look at the next item in the linked list and add it to the hash table at the bucket the same value. A list of 2,2,4,4 would have 2,2 at bucket 2 and 4,4 at bucket 4. Then I try to remove items that appear multiple times in the second loop. Commented Aug 18, 2014 at 8:43

2 Answers 2

3

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)
{
     ...
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.