Efficiently typecast elements of a vector in Java -


What is the more effective way (preferably o (1) than o (n), but at least speed Type ) Type the elements of this sector?

  public vector & lt; String & gt; TypecastVector (vector & gt; Object & gt; objects) {vector & lt; String & gt; Wire = new vector & lt; String & gt; (); For (object o: objects) string. Add (string) o); Return strings; }  

note

Anyone who is running in a clear requirement of the vector or other generic class type: acceptable answer points As outside, this is probably one and you probably need code in your class hierarchy.

especially , if you do not already have it, you should consider creating those sections which are said to be vector or other Generic classes themselves use when I did this in my code, so I completely eliminated the need for the function in my code which was in accordance with the above work.

If you have never implemented the generics in your code, keep an eye out for the "Generics" link above. You may be surprised (as I was) to find out that they can be used to properly implement the functionality that you feel you need from the vector typecast.

If only the thing you want to do is vector and lieutenant; Object> Vector from & lt; String is what you can do. You have to make sure that every object in your vector is a string!

Obviously, this will not work:

  vector & lt; Object & gt; ObjectVector = new vector & lt; Object & gt; (); Of vector & lt; String & gt; StringVector = (vector & lt; string & gt;) objectVector;  

But you can do this:

  vector & lt; Object & gt; ObjectVector = new vector & lt; Object & gt; (); Vector typelessVector = objectVector; Of vector & lt; String & gt; StringVector = (vector & lt; string & gt;) typelessVector;  

You will receive some alerts, but the code should work properly.

As mentioned earlier, this code feels like odor.


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -