ArrayStoreException when trying to save an array of linked list
I am trying to create a linked list array that will handle hash collision. I have an array of objects that I tried to store linkedList
, but it doesn't work. Is there something I am doing wrong, or is there a better route I should take.
This is the error I am getting when I run my code.
Exception in thread "main" java.lang.ArrayStoreException: linkedList
at p7b.Insert(p7b.java:57)
at p7b.main(p7b.java:31)
Here is my method
public static void Insert(int hashVal, String key,int[] arrayNums, Object[] arrayString){
Node newNode = new Node(key,null);
linkedList list = new linkedList();
if (arrayString[hashVal] == null){
arrayString[hashVal] = list;
}
Here is the code linkedList
:
public class linkedList{
private Node head;
private int size;
public linkedList(){
size = 0;
head = null;
}//end default constructor
public boolean isEmpty(){
return size == 0;
}//end isEmpty
public int size(){
return size;
}//end size
protected Node find(int index){
Node curr = head;
for(int skip = 0; skip < index; skip++){
curr = curr.getNext();
}//end for
return curr;
}//end find
public Object get(int index){
if (index >=0 && index < size) {
// get reference to node, then data in node
Node curr = find(index);
Object dataItem = curr.item();
System.out.println(dataItem);
return dataItem;
}
else
return "error";
}//end get
public void add(int index, String item){
if(index>= 0 && index < size+1){
if(index == 0) {
//insert the new node containing item at
//beginning of list
Node newNode = new Node(item,head);
head = newNode;
head.setNext(head);//--------------------
}
else {
Node prev = find(index-1);
//insert the new node containing item after
//the node that prev references
Node newNode = new Node(item,head.getNext()); //changed prev to head
head.setNext(newNode); //prev.next = newNode --//changed prev to ead
head = newNode;
}//end if
}
sizeplus();
/*if(index == 16){//fffff
for(int i = 0; i < 50; i++){
System.out.println(head.item());-------------EXR
head = head.getNext();
System.out.println("----------");
}//ifffffff
}
*/
}//end add
public int sizeplus(){
size+=1;
return size;
}
public void remove(int index){
int num = index;
while(size>0){
//System.out.println("strt");
if(index>= 0 /*&& index < size*/){
if(index == 0) {
//delete the first node from the list
System.out.println("REMOVED :"+head.item());//----------------EXR
head = head.getNext();
}
else {
Node prev = find(index-1);
// delete the node after the node that prev
//references, save regerence to node
Node curr = prev.getNext();
System.out.println(curr.item());
if(size > 1){
}
else{
System.out.print("is the last one left");
}
prev.setNext(curr.getNext());
curr = prev.getNext();
index+=num-1;
}//end if
size--;
//System.out.println(size+" <-size || index-> " +index);
}//end if
}
}//end remove
public Node getH(){
return head;
}
}//end class
ALSO: how I go from the linked list to the next one here is what I tried.
linkedList list = new linkedList();
list = arrayString[i];
p7b.java:44: error: incompatible types
list = arrayString[i];
^
required: linkedList
found: Object
1 error
source to share
From your main method:
Object[] arrayString = new String[40];
...
Insert(hashVal,key,arrayNums,arrayString);
You are passing a String array to your method. Therefore, you cannot put instances linkedList
in this array.
If you want this array to contain instances linkedList
, change the code to:
Object[] arrayString = new linkedList[40];
...
Insert(hashVal,key,arrayNums,arrayString);
or even better:
linkedList[] arrayString = new linkedList[40];
...
Insert(hashVal,key,arrayNums,arrayString);
and change the signature Insert
to accept linkedList[]
instead Object[]
.
source to share
Here's the culprit:
Object[] arrayString = new String[40];
You claim it is Object[]
, but it is actually supported String[]
. Java cannot make the definition at compile time so you cannot store the old one Object
in that array, so it explodes at runtime.
What you need to do is enter the array correctly - if it will contain objects linkedList
, it must be linkedList
entities.
linkedList[] linkedLists = new linkedList[40];
In particular, you are using an array in different places. Observe its use.
Having a few thoughts on your code: this is not ... correct. You are creating a linked list, right? There is no reason to use arrays unless the list itself is supported by them.
I can't vouch for the correctness of your method linkedList.add
right now, but you don't want to use arrays for this at all. Create one instance of the linked list outside of this method and store whatever data you need.
public static void insert(String key, int index, linkedList list) {
list.add(index, key);
}
(Typically, linked lists don't have index positions that you just insert, so the part is index
completely redundant.)
source to share