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 8503797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:51:17+00:00 2026-06-11T01:51:17+00:00

I want to implement the following function: public boolean checkType(Vector<?> vec) { // return

  • 0

I want to implement the following function:

public boolean checkType(Vector<?> vec)
{
  // return true if its Vector<Integer> and false otherwise
}

How can I check the vector elements type ?
Note that the vector might be empty thus I can’t check if the first element is “instanceof” Integer or String …

EDIT:

Well, I had something in mind I don’t know if it will work or not

Can I implement the checkType function as following:

public <T> boolean checkType(Vector<T> vec)
{
  // return true if T is Integer and false otherwise
}

is it possible to check if T is Integer ?!

Thanks in Advance

  • 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-06-11T01:51:19+00:00Added an answer on June 11, 2026 at 1:51 am

    Generic type parameters are unrecoverable (except for some special cases) at runtime because of type erasure. This means that at runtime, both Vector<Integer> and Vector<String> are simply just Vectors and their elements are just Object references.

    Only the actual runtime classes (discoverable by instanceof checks as you pointed out) of the individual elements make the notion of the element type, otherwise the Vector itself has no idea what it’s element type is.

    So basically, an empty vector of any type is equal to empty vector of any other type. It is even safe to cast it like this:

    Vector<String> noStrings = (Vector<String>) new Vector<Integers>();
    

    However there is one problem, because although you can say that empty vector conforms to any required element type, this statement stands only as long as the vector stays empty. Because if you do this:

    Vector<Integer> ints = new Vector<Integer>(); // empty
    Vector<String> strings = (Vector<String>) ints; // unchecked warning, but still possibly ok
    ints.add(1); // here comes trouble
    String s = strings.get(1); // oh oh, ClassCastException
    

    EDIT:

    To answer you second question: No it isn’t possible to write anything like this:

    public <T> boolean checkType(Vector<T> vec) {
        return T instanceof Integer; // impossible
        return T == Integer; // impossible
        return T.class == Integer.class // impossible
        return vec instanceof (Vector<Integer>); // impossible
    }
    

    However you can write a method that uses a class token as an input parameter:

    static <T> boolean checkElementType(Collection<?> collection, Class<T> elementType) {
        for (Object object : collection) {
            if (!elementType.isAssignableFrom(object.getClass())) {
                return false;
            }
        }
        return true;
    }
    

    And then you can use it like this:

    List<?> list1 = Arrays.asList(1, 2, 3);
    List<?> list2 = Arrays.asList(1, 2, 3, "a");
    System.out.println(checkElementType(list1, Integer.class)); // true
    System.out.println(checkElementType(list2, Integer.class)); // false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form (form2) and I implemented the following PUBLIC method: function ShowInterface(i:integer):boolean;
I want to implement this function: Public Function GetFunc(Of TSource, TResult) _ selector As
Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>
In an application having UITabBarController , I want to implement following scenario: When user
I want to implement the following constraints in mysql: create table TypeMapping( ... constraint
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
I want to implement a function that accepts a DbSet (non-generic), a string ,
I want to implement a function tracer, which would trace how much time a
I want to specialize a class template with the following function: template <typename T>
I have the following multithreading function to implement threads fetching from a list of

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.