2

I have an assignment and I need to make my own (simple) generic linked list:

public class Node<T> {

   private int key;
   private T data;
   private Node<T> nextNode;

}

But I need to implement a dictionary with a hash table. I wanted to make a list containing Node(s). In case of a conflict (two objects of type disperse to the same node, I simply link them - linked lists).

I have to implement this by myself, no outside help (already implemented lists or what ever)

How I wanted to do this:

public class GenericDictionary<T> implements GenericDictionary_interface<T> {

    private int capacity;   
    private Node<T> [] slots;

    public GenericDictionary () {   
        this.capacity = 31;
        slots = new Node<T>[capacity];  // the array I need which I disperse to
    }
}

This however is not exactly possible. I did try and read on the subject, tried searching here on SO ... but I didn't get it at all.

My only request is ... don't be lazy on variable / method names, make them easy to understand please.

8
  • @nfechner The homework tag has been deprecated. Commented Oct 24, 2012 at 10:32
  • 1
    If you have to implement this by yourself, without outside help, why do you ask? Isn´t that requesting outside help? Commented Oct 24, 2012 at 10:35
  • @TheBlastOne. Common, there is nothing wrong with getting some help if OP has tried out code. Had he asked to write code for him. That would be wrong. Commented Oct 24, 2012 at 10:37
  • @RohitJain Agree. I just wonder if OP understands he´s breaking the rule. Surely we don't have a problem with that. But he should. I'd say he should question the rule, since getting (and accepting) hints or help from outside, and accepting it, is a key to learning. Commented Oct 24, 2012 at 10:41
  • 1
    @Kalec all I say is a) don't just break the rule, destroy it! because b) I agree that you are doing nothing wrong when you're asking for the right help. Commented Oct 24, 2012 at 12:47

1 Answer 1

5

Here's the best you can do:

    @SuppressWarnings("unchecked")
    Node<T>[] slots = (Node<T>[]) new Node<?>[capacity];

You can't get rid of the warning (aside from suppressing it). When you need an array of a generic class, you need to create the array with unspecified generic type then cast it.

Sign up to request clarification or add additional context in comments.

6 Comments

So I'm typecasting Node<?> as Node<T> ? and thus it would work ? Okay, any way it could backfire ?
None. You need to understand that unlike C++/C# templates which generate actual implementations, Java generics are just extra syntactic compile time checks and automatically inserted casts. For instance ArrayList<Integer> and ArrayList<String> are the same class, the same object even. Generic type only changes the cast used before returing the element on get(), Object array is used in both cases.
Another example to help you understand the nature of Java Generics: You can fill List<Object> with 10 strings and 1 integer, cast it to List<String> and the list will work as long as you don't try to get() the integer element (because get() method has code like this return (T)obj_array[idx]; which will cause class cast exception in List<String> when the actual element is integer.
Okay, I understand. I do come from a background of C/C++ so I didn't EXACTLY understand how it works, thank you.
|

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.