• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Vector and ArrayList Behavior got me in a loop...

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. If someone could please tell me why I am seeing different results with the following code:

/**
* This works fine...
*/
SomeObject [] s = new SomeObject[2];

s[0] = new SomeObject();
s[1] = new SomeObject();
s[2] = new SomeObject();

s[0].setSomething("Some value here");
s[1].setSomething("Another value here");
s[2].setSomething("And yet another value here");

/**
* But this only stores the last value in each of the array
* elements
*/
String [] SomeArray = {"one", "two", "three"}
int numOfObjectsNeeded = 3
SomeObject [] t = new SomeObject();

for (int i=0; i<numObjectsNeeded; i++) {
t[i] = new SomeObject();
t[i].setSomething(UniquePrimeativeValueFromSomeArray[i]);
}
/**
* Now if I look into each of the three object array elements, I
* see that "three" has been written to all of them!
*/

The first part works just fine. Ive even tried it with Vector() and ArrayList() but I get the same results. I'm obviously doing something wrong... any ideas?

Thanks for reading,
Tommy T
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have anything declared static in the code that isn't visible?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Tommy.
Can you post some code that compiles and exhibits the behavior you are observing? There's a couple of problems with the above code that makes me think we aren't seeing the whole story.
 
Tommy Thomasson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Much thanks to both Keith and Joe! I feel like I owe you guys money after the amount of time i spent trying to figure out what the was going on with my code (the check's in the mail). The class variable in the SomeObject class was set as static. Upon removing the static modifer, my code ran as intended. Upon reviewing the code, I'm not sure what I was thinking when I declared all of the class variables as static (there was about 15 of them)

Again, the help was much appreciated.
 
Don't MAKE me come back there with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic