Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 988345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:37:30+00:00 2026-05-16T05:37:30+00:00

Is there a more efficient way (preferably O(1) rather than O(n) but at least

  • 0

Is there a more efficient way (preferably O(1) rather than O(n) but at least faster to type) to typecast the elements of a vector than this?

public Vector<String> typecastVector(Vector<Object> objects){
    Vector<String> strings = new Vector<String>();
    for(Object o : objects)
        strings.add((String) o);
    return strings;
}

Note

To anyone who is running into an apparent need to typecast a Vector or other Generic class: as the accepted answerer points out, this is probably a code smell and you probably need to refactor the code in your class hierarchy.

Specifically, if you haven’t already, you should consider making the classes that use said Vector or other Generic class use Generics themselves. When I did this in my own code, I completely eliminated the need for the function in my code that was analogous to the above function.

If you’ve never implemented Generics in your own code, check the “Generics” link above. You may be surprised (as I was) to find that they can be used to implement precisely the functionality you thought you needed from a Vector typecast.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:37:30+00:00Added an answer on May 16, 2026 at 5:37 am

    If the only thing you want to do is to cast from Vector<Object> to Vector<String>, that you can do. You’ll have to be sure every object in your vector is a String though!

    Obviously, this won’t work:

        Vector<Object> objectVector = new Vector<Object>();
        Vector<String> stringVector = (Vector<String>)objectVector;
    

    But you can do this:

        Vector<Object> objectVector = new Vector<Object>();
        Vector typelessVector = objectVector;
        Vector<String> stringVector = (Vector<String>)typelessVector;
    

    You’ll get some warnings, but the code should work fine.

    As was mentioned before, this does feel like a code smell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a more efficient way to output a command than this: whereis python
Is there a more efficient way to clamp real numbers than using if statements
Is there are more efficient way than the following for selecting the third parent?
Is there any time in which a reference type is more efficient than a
Is there a more efficient way to write this code? Or with less code?
Is there a more efficient way to add objects to an NSMutable Dictionary than
Is there a more efficient way to handle this? List<String> lstReferences = (from f
Is there a more efficient way than <div style=height: 6px;></div> To make a narrow
Is there a more efficient way for doing this? for item in item_list: e,
Is there a more efficient way to do this without counting twice? i.e returning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.