A small problem with vectors
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   prachi_jain
Posted On:   Sunday, August 4, 2002 07:32 AM

Hii friends, I am having a problem....problem goes like this...i m having two vectors say A and B. Both of same capacity. "Vector A" i stored some strings say A1, A1, A2, A2, A3 "Vector B" values are 10, 20, 5, 7, 20 i just want to have the result in two other vectors as "FinalAVector" A1, A2, A3 "FinalBVector" 20, 7, 20 I hope u understand, i need the max of values from the problem. I m trying to do that...but, i m failing in getting the max values in final vector. It's a very small problem..i hope someone can help me to do this. thanks.    More>>

Hii friends,

I am having a problem....problem goes like this...i m having two vectors say A and B. Both of same capacity.

"Vector A" i stored some strings say A1, A1, A2, A2, A3
"Vector B" values are 10, 20, 5, 7, 20

i just want to have the result in two other vectors as

"FinalAVector" A1, A2, A3
"FinalBVector" 20, 7, 20

I hope u understand, i need the max of values from the problem. I m trying to do that...but, i m failing in getting the max values in final vector.

It's a very small problem..i hope someone can help me to do this.

thanks.

   <<Less

Re: A small problem with vectors

Posted By:   Eric_Lindauer  
Posted On:   Sunday, August 4, 2002 08:34 PM

Of course there must be a million ways to do this, but here's one: create a Map using your strings ( A1, A2, A3 ) as keys, and the largest value ( 10, 20, etc ) as values. Loop through your two vectors, putting a new integer into the map iff it is greater than the current value, ie:



public void sort () {
Map result = new Hashtable ();
for ( int i=0; i < stringVector.size (); i++ ) {
String key = stringVector.get (i);
Integer value = intVector.get (i);
if ( result.get (key) != null ) {
Integer current = (Integer) result.get (key);
if ( value.intValue () > current.intValue () ) {
result.put ( key, value );
}
} else {
result.put ( key, value );
}
}

// ... now you can use the map to produce whatever
// vectors you need
Collection uniqueKeys = result.keySet ();
Collection largestInts = result.valueSet ();

// ... etc ...
}



Hope this helps.

Eric

Re: A small problem with vectors

Posted By:   Jorgen_Nordqvist  
Posted On:   Sunday, August 4, 2002 07:47 PM

It would probably help if you pasted a code snippet here to show what the code currently looks like.

Jorgen

About | Sitemap | Contact