• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector usage question

 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a vector to implement a hashtable (I cannot use built in hashtable). I am using "hashTable.contains(search key)" to see if the key in question already exists in the vector (I only insert unique keys). If this test passes I run my hash function and attempt to insert the key in its respective index.
My question: Is there a way to go directly to a certain index and test if the value in it is null, instead of going through the vector one index at a time until I get to that index? If it is I will insert into that index using "hashTable.insertElementAt(index)", if it is already taken I will double hash and then do the same test with the new index. If there is a built in method that does this, what is it, how is it used and what does it return?
Thank You
E Fox
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean like Vector.elementAt(int) ? This method returns the entry at a given index which can be null.
 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a method to insert keys into a vector, this is the method I have written at the moment:
public void insertIt(String lineIn, Vector hashTable){
index = ELFhash(lineIn, 17);while (hashTable.elementAt(index) != null){ long newIndex = ELFhash(lineIn, 7);
index += newIndex;
}
lineIn.insertElementAt(index);System.out.println(lineIn + " inserted at index " + index);
return;
}
I call the method in main() with :

if (!(hashTable.contains(lineIn))){
insertIt(lineIn, hashTable);}
else System.out.println(lineIn + " already in table");
I get a few errors stating that say:
"cannot resolve symbol : lineIn.insertElementAt(index);" with a carrot pointing to the period
"vector cannot be applied to long"
"method insertIt cannot be referenced from a static context"
I am fairly new to vectors and how to insert and implement them. What needs to be fixed in my insertIt method to get everythig=ng working?
Sorry about the long post.
Thank You
E Fox
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't tell you exactly what to do based on just these code fragments, but there are some fairly basic conceptual Java errors here, nothing really to do with Vectors in particular. The "from a static context" message means that in main, which must be a static method, you can't call the non-static method insertIt() from the same class without some object of this class to call it on. In insertIt(), it looks like the variable index is a member variable, so changing insertIt() to be static isn't the right solution; you need to create an instance of the class.
If you don't immediately smack your forehead here and say "Doh! of course!" then the best thing for you to do, in all seriousness, is to back up and learn some more of the fundamentals of Java. You'll need a good introductory Java book; Bruce Eckel's "Thinking in Java" is one, but there are many others. If you keep trying to write this program without understanding more of the basics, the result is not going to be something you'll be proud of.
 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, I was able to fix my errors. I have a question with may be stupid: if I use "hashTable.elementAt(index)" and nothing is at the index will it return null so that I can use "while (hashTable.elementAt(index) != null)" to check if an index is used and if it is used execute the loop? Will this work?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but remember that the Vector has a set size -- if your loop finds every element is non-null, then it will eventually try to access an element off the end of the Vector and you'll get an IndexOutOfBoundsException (or something along those lines.)
 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was the problem I was running into. After looking at the code I came to the conclusion that I made the new vector in the wrong place. I have it directly under main(). I can use hashTable.addElement() just fine but not hashtable.insertElementAt() because the vector it is inserting into has size 0, even though I used "final Vector hashTable = new Vector(20000, 500);" Where should I put this statement?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK. The constructor argument "20000" is not the "size" of the Vector, it's the "capacity" of the Vector. Inside the Vector object is an array; the capacity is the length of that array. The size, though, is the number of valid elements the Vector thinks are in that array. Even if you specify that the capacity is 20000, the size still starts out at 0.
If you want a Vector that thinks its holding 20000 null elements, then construct it just as you are, but then call setSize(20000) on it afterwards. This tells the Vector to assume that the first 20000 entries in the array are valid.
 
E Fox
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply. I have another question concerning vectors. I am using insertElementAt() to insert the keys at their indexes that are gotten from a hash function. The problem I run into is if somethng is inserted into index or any number and then the next key is inserted at a lower index, say 4. The vector erases the key at the higher index and in one instance where I had inserted at 11 then 14 and then into 4 the key at 11 was erased and the key at 14 was shifted down to 15. I read in the api " Each component in this vector with an index greater or equal to the specified index is shifted upward to have an index one greater than the value it had previously. " Is this what is happening and if so is there a way to insert directly to an index without the previous keys being lost?
Thanks
E Fox
[ July 27, 2003: Message edited by: E Fox ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to ask you two questions before I answer yours:
  • Why are you writing your own hash table from scratch?
  • Given that you have a good answer for #1, why are you using a Vector instead of just using an array?

  • Now, to answer your question directly: setElementAt() (also set(), same thing) sets a particular element without moving any existing ones. insertElementAt() does, indeed, move all the higher-indexed elements.
    Why not spend ten minutes reading all of the java.util.Vector javadocs? If you read the whole page, you'd have been able to answer most of the questions in this thread by yourself. It's definitely worth your time! See, for example, this copy on Sun's web site.
     
    E Fox
    Greenhorn
    Posts: 16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    For this assignment we are required to write our own hash table. I have used a vector because I am not told how many strings I will have to deal with, so I chose a vector so that it can resize itself if there is not enough space in the original vector. The sample data files I was given are huge and have a few thousand unique strings.
    Sorry about all the questions, especially the last, I totally overlooked set() when I read through the javadoc earlier.
    [ July 27, 2003: Message edited by: E Fox ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic