• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper Syntax For Removal From Vector  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to know the proper syntax for removal of an element from a vector. Do I have this correct?
Example:
I have taken a look at the Vector API but I don't quite understand the proper format for this code. Could someone take a look and see if I've got this right?
I have an object that contains a Vector of Strings named dataType.
I have an int variable named elementNumber that specifies the location of the element I want to remove.
Would my method call to remove the String Object look like this?
dataType.remove(elementNumber)
Thanks,
Landon
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an int index into the Vector (of Strings), you could use:

On a side note, I assume since you're using a Vector that you're working with legacy code. On new projects, it's much better to use the Collections API. ArrayList is a direct substitute for Vector.
Cheers,

- Tim
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim...I'll check out array list.
What does the second line of code in your example accomplish? Does it store the value of the element removed from the Vector in the String myVal?
Thanks for your help.
Landon
 
Tim West
Ranch Hand
Posts: 539
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second says "remove the element at index <foo>, but give me a reference to it".
So if you wanted to remove an element but then do some manipulation on that element, it might be handy.
Without checking, I'd guess the following two snippets achieve an identical result:

and

BTW I see you posted your original Q in Beginner too - cross-posting is generally frowned upon .
Cheers,

--Tim
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!