Author Topic: SimpleVector in collection!!!  (Read 2389 times)

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
SimpleVector in collection!!!
« on: September 26, 2006, 01:08:22 am »
Hello

I store few SimpleVectors in collection class Hashtable like that

Code: [Select]
protected Hashtable places = new Hashtable();

   public void updateKoordinates()
   {
         SimpleVector place = part.getOrigin();
         place.add(new SimpleVector(-50, -55, 80));
         places.put("0001", place);
         place = part.getOrigin();
         place.add(new SimpleVector(-50, -55, -420));
         places.put("0002", place);
         place = part.getOrigin();
         place.add(new SimpleVector( 50, -55, -420));
         places.put("0003", place);
         place = part.getOrigin();
         place.add(new SimpleVector( 50, -55, 80));
         places.put("0004", place);
    }

   public SimpleVector placePos(String placeNr)
   {
      System.out.println(places.get(placeNr));
      return (SimpleVector)places.get(placeNr);
   }    



The method updateKoordinates() is called only once.
After some time of runing my application and calling method placePos(placeNr)... SimpleVector cordinates are changed. Anyone has idea what could cause that??

Thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: SimpleVector in collection!!!
« Reply #1 on: September 26, 2006, 05:15:19 pm »
Quote from: "eye1"
Anyone has idea what could cause that??
Yes...you!  :wink: You are modifying the SimpleVectors somewhere, may it be an implicit or explicit modification. Replace
Code: [Select]
return (SimpleVector)places.get(placeNr);
with
Code: [Select]
return new SimpleVector((SimpleVector)places.get(placeNr));
and the SimpleVectors in the map should remain unchanged.

Offline eye1

  • int
  • **
  • Posts: 68
    • View Profile
SimpleVector in collection!!!
« Reply #2 on: September 26, 2006, 10:04:38 pm »
Thank you Egon!

What a silly mistake  :oops:  :oops:

And i was reducing nr. of threads in my application, using only synchronized collections, synchronizing iterators and in the end it was so simple.   :oops:  :oops: . And i found the place where i was changing SimpleVector.

Thanks again!!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
SimpleVector in collection!!!
« Reply #3 on: September 26, 2006, 11:04:08 pm »
No problem. I'm always glad if i can help. Remember that everything that i can comment on is something that i did wrong myself in the past... :wink: