0

I saw all the past Questions in StackOverflow, I don't find an appropriate answer of my Question. I want to create a Array of List of class Association, but after execution, I have an Exception like that in the line :

private static final int SIZE=99999; 
private List<Association <K,V>> [] hashtab; 
hashtab = (List<Association    <K,V>>[]) new Object [SIZE];

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.util.List; at com.code.classes.TableHachage.(TableHachage.java:20) at com.test.TestHash.main(TestHash.java:11)

How can I fix that?

NB: It's a academic problem, so we can't use directly HashTable, we have to use Collision resolution by chaining, and use LinkedList for that

azurefrog
  • 10,785
  • 7
  • 42
  • 56
Mourad BENKDOUR
  • 879
  • 2
  • 9
  • 11
  • You don't seem to understand how casts work in Java. You can't just change the type of an object with a cast. Why aren't you making what you actually want (`new ArrayList>()[SIZE]`) instead of creating an `Object[]` and trying to cast it? – azurefrog May 01 '15 at 17:36
  • See http://stackoverflow.com/questions/5289393/casting-variables-in-java for a more detailed explanation. – azurefrog May 01 '15 at 17:39
  • Please @azurefrog, I tried that before post my question, new ArrayList>()[SIZE] not work, there is Unresolved compilation problems: The type of the expression must be an array type but it resolved to ArrayList> – Mourad BENKDOUR May 01 '15 at 18:27
  • In the future you should include things that you have already tried in your question when you ask it. That will save people from suggesting things you have already tried and save everyone's time. – azurefrog May 06 '15 at 19:16

3 Answers3

0

Why not just: hashtab = (List<Association<K,V>>[]) new ArrayList[SIZE]; ?

Actually you do not understand elementary principles of OOP: objects of class Object can not be casted to any other class, because Object is super class for all others.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
0

Not sure why you are trying to do the casting? That is your problem.

try just doing a new array list: List listOfAssociations = new ArrayList();

Let me know what you are trying to do with the "hashTable". Because you could use a HashMap or some other object

(I am kind of confused with what you are trying to achieve)

andres
  • 1
  • 1
0

Use either

hashtab = new ArrayList[SIZE];

or

hashtab = (List<Association <K,V>>[])new ArrayList<?>[SIZE];
newacct
  • 119,665
  • 29
  • 163
  • 224