Problem in reading vector
Hello,
I use a vector to store hash table, storage is done without problems. But when I browse the vector, I returned an item that does not seem to exist in the vector. The problem is in reading the vector. Here is my code
Code:
/ / write
if (new_entry) {
tps.addElement(tp);
System.out.System.out.println("Elements read in the 1st loop" + Topics.elementAt(f));
f + +;
tp.clear();
...
}
/ / reading
for (int m = 0; M <tps.size(), M + +) {
System.out.System.out.println("========= "+ " " + Topics.elementAt(m));
}
Re: Problem in reading vector
Hi,
Because you have to display immediately after changing the value of the subject. There is no mystery if you want multiple items? So we must create multiple objects. I remember that unlike C + +, there is no copy implicit in Java. So, you have to try other alternatives than this. Just try some of them and if you have any problem do post back.
Re: Problem in reading vector
Hello,
Gaps exist in your code snippet so we can see what you do:
- Where does the object "tp"?
- What is the function tp.clear ()?
Already we can see that in the first loop, just after posting an item, you change it (topic.clear ()) So it starts bad if you expect to get the same result on the display. Then, as stated above, we do not risk to guess what you do outside of a few lines of code shown ... If it is you use always the same object topic.
Re: Problem in reading vector
Hello,
If you change the subject "tp", Then necessarily it will be modified. As for "copy the" what does that mean for you? You have an object you put in a vector. Well it is this subject, this forum, which is stored in the vector. If you do not use a constructor or method "copy" or "clone" (which itself call a constructor) no new object will be created. Try this and if you have any problem then do post back.
Re: Problem in reading vector
Hello,
C, what is the difference? In fact I use Vector just because I need to store items that I know the number, so I need a structure that resembles the tables but without knowing the size a prior. Thank you, my confusion. I thought that when I do a addElement (object), a new object is constructed automatically. I knew that it pointed to the added object. I tried with clone and it works
Code:
topics.addElement(topic.clone());
topic.clear();
Re: Problem in reading vector
Hi,
Vector date of the beginning of Java, and has two great flaws:
-it is synchronized (and therefore potentially less performance if you use mono-thread).
- it has an API not very clear, with great method
Basically you replace it with this:
Code:
List topics <TaClasseTopic> = new ArrayList <TaClasseTopic>();
then you use add () instead of addElement ()...
When the iterator and loop view of Java 5, this allows you to browse all the elements like this:
Code:
for (TaClasseTopic topic: topics) {
System.out.System.out.println("========= "+ " " + Topic);
}