Results 1 to 6 of 6

Thread: Problem in reading vector

  1. #1
    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));
    }

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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();

  6. #6
    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);
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,900,967.08807 seconds with 17 queries