Click to See Complete Forum and Search --> : vectors


Rabbit3cat
11-14-2005, 07:39 AM
Hi ive a question about vectors. I have two vectors each containing the same string of 8 words:
ONE TWO THREE THERE
FOUR FIVE SIX SEVEN
and i am comparing the two:

int sameWord = 0;
for(int i = 0; i < vector1.size(); i++)
{
Object a = vector1.elementAt(i);

for(int j = 0; j < vector2.size(); j++)
{
Object b = vector2.elementAt(j);
if(a.toString().compareTo(b.toString()) == 0){
sameWord++;

}
}
}

the result however when comparing the two is that 9 of these words are the same whereas i would expect it to be eight since there are only 8 words in each string and both vectors contain the same string. Does anybody know why this result would be 9?
Thanks

Oak
11-14-2005, 07:58 AM
The result is actually 8 as you will see when running the attached file.

I am not sure how you were getting 9 but it makes no sense. Do you have a screen shot that proves you were getting 9?

Khalid Ali
11-14-2005, 08:47 AM
Object b = vector2.elementAt(j);
if(a.toString().compareTo(b.toString()) == 0){
sameWord++;

...... i would expect it to be eight since there are only 8 words in each string and both vectors contain the same string. Does anybody know why this result would be 9?
Thanks

could it be that you have initialized sameWord=1 in the beginning somewhere?

Oak
11-14-2005, 09:06 AM
I doubt it since he probably used copy and paste to add the code to the post.

Khalid Ali
11-14-2005, 04:59 PM
if u are right then he certainly has more values int he vector then he expects

Oak
11-15-2005, 05:07 AM
Yeah, from the code he/she posted that is that only thing we can assume. I have a feeling he/she has already figured it out :P