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.

assylias
  • 321,522
  • 82
  • 660
  • 783
Kalec
  • 2,681
  • 9
  • 30
  • 49
  • @nfechner The [homework tag has been deprecated](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated). – assylias Oct 24 '12 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? – TheBlastOne Oct 24 '12 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. – Rohit Jain Oct 24 '12 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. – TheBlastOne Oct 24 '12 at 10:41
  • @TheBlastOne. Agree with that :) – Rohit Jain Oct 24 '12 at 10:42
  • @TheBlastOne The point is for us to know how lists actually work, not just read about list.add(item), rather to implement it ourselves. Yes it is also to see how we overcome obstacles like the one I mentioned ... but we are not required to know every in and out of generic items by next week. I see no problem with asking for help as long as I'm actually asking for help, not asking for code. If you find it as being "cheating", no one is forcing your hand to help me. – Kalec Oct 24 '12 at 11:07
  • 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. – TheBlastOne Oct 24 '12 at 12:47
  • @assylias Thanks for the info. I didn't know.. Good riddance anyway.. – nfechner Oct 24 '12 at 13:53

1 Answers1

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.

RokL
  • 2,663
  • 3
  • 22
  • 26
  • So I'm typecasting Node> as Node ? and thus it would work ? Okay, any way it could backfire ? – Kalec Oct 24 '12 at 10:26
  • 1
    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` and `ArrayList` 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. – RokL Oct 24 '12 at 10:29
  • 1
    Another example to help you understand the nature of Java Generics: You can fill List with 10 strings and 1 integer, cast it to List 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 when the actual element is integer. – RokL Oct 24 '12 at 10:34
  • 1
    This is called "type erasure": http://docs.oracle.com/javase/tutorial/java/generics/erasure.html – John B Oct 24 '12 at 10:58
  • Okay, I understand. I do come from a background of C/C++ so I didn't EXACTLY understand how it works, thank you. – Kalec Oct 24 '12 at 11:11
  • I should mention that while your answer is correct in a general way, I had to modify it. I used `@suppressWarnings("unchecked")` before the method and `this.slots` instead of `Node[] slots = etc` – Kalec Oct 24 '12 at 11:37