aspose file tools*
The moose likes Java in General and the fly likes Vector usage question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector usage question" Watch "Vector usage question" New topic
Author

Vector usage question

E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

You mean like Vector.elementAt(int) ? This method returns the entry at a given index which can be null.


[Jess in Action][AskingGoodQuestions]
E Fox
Greenhorn

Joined: Jul 24, 2003
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jul 24, 2003
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jul 24, 2003
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

Joined: Jul 24, 2003
Posts: 16
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

Joined: Jul 08, 2003
Posts: 24184
    
  34

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

    Joined: Jul 24, 2003
    Posts: 16
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Vector usage question