Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Problem in reading vector

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 03-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
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));
}

Reply With Quote
  #2  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 330
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.
Reply With Quote
  #3  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 333
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.
Reply With Quote
  #4  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 518
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.
Reply With Quote
  #5  
Old 03-02-2010
Member
 
Join Date: Dec 2009
Posts: 296
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();
Reply With Quote
  #6  
Old 03-02-2010
Member
 
Join Date: Nov 2009
Posts: 446
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);
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem in reading vector"
Thread Thread Starter Forum Replies Last Post
Reading from file and assigning it to a vector Macbeth Software Development 5 19-10-2010 02:44 PM
Function problem with Vector Class Vodka Software Development 5 12-02-2010 12:52 AM
Problem in reading stream Windowed Windows Software 5 06-01-2010 11:15 AM
Problem with vector definition DotNetUser Software Development 3 14-05-2009 01:22 PM
Problem reading USB stick - Empty Shell - Portable Devices 3 05-02-2009 08:25 PM


All times are GMT +5.5. The time now is 11:19 PM.