Need Help With Vectors

Hi,

I need to do a comparison between 2 vectors and remove duplicates from the second vector.

Here's how I went about it

Vector textArrays = new Vector();
Vector initiatedArray = new Vector();
Vector deliveredArray = new Vector();

----
Other Code
-----

textArrays = doCompareArrays(deliveredArray, initiatedArray);

----
More Code
-----

// The section which actually compared the vectors is in a method called doCompare and goes as below

Vector doCompareArrays (Vector Vector1, Vector Vector2) throws IOException
{
counterVariable = 0;
int tempCounter = 0;
while (counterVariable < Vector1.size())
{
String str1 = (String)Vector1.get(counterVariable ++);
tempCounter = 0;
while (tempCounter < Vector2.size())
{
String str2 = (String)Vector2.get(tempCounter ++);
if (str1.compareTo(str2) == 0)
{
Vector2.remove(-- tempCounter);
//-- counterVariable;
}
}

}
return Vector2;
}


// My problem is that the doCompare method modifies my vector which I provided it as input, the "initiatedArray". Why does it do that? Is there any way around this?

Thanks
Joe

Comments

  • : // My problem is that the doCompare method modifies my vector which I provided it as input, the "initiatedArray". Why does it do that? Is there any way around this?

    This is a reference variable: [b]Vector v[/b] (as opposed to a primitive like [b]int[/b]). v points to an object on the heap (or null). So you are passing in a reference to the object, modifying the object and returning a reference to the object.

    So, you need to create a new Vector inside your method and return a reference to this as the result.


    Also, I don't see why your method throws an IOException; you may have a reason for this.

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion