How can I get the length of a generic data structure without knowing the type of data structure it will be?

Kahl :

I have created my own data structure that functions similarly to an ArrayDeque and extends an AbstractList. The point of it is to hold other data structures, but not allow structures that have more than a specified number of elements.

For example (my constructor):

public MyD(Class<T> t, int limit) 
{
}

Where limit determines the amount of elements allowed per data structure added to the ArrayDeque.

My issue is that, because I'm using generics, I don't technically know what type of data structure I'm working with at the time.

public void add(int i, T structure) {}

Where structure could be anything from an array, to ArrayList, to Queue, etc.

My question: Is there a way to take a generic value and generically cast it to get the amount of elements within if I know it will always be some form of data structure.

I have tried casting it to a simply array[], but if a List is passed through, it will break the functionality.

@SuppressWarnings({"unchecked"})
T[] array = (T[])x;

if(array.length > b) { return; }
Kirill Simonov :

You could use some reflection to test whether the passed structure is an array or a Collection and then get its size accordingly:

public void add(int i, T structure) {
    int size;
    if (structure.getClass().isArray()) {
        size = Array.getLength(structure);
    } else if (structure instanceof Collection) {
        size = ((Collection) structure).size();
    } else {
        throw new IllegalArgumentException("Structure is neither an array nor a collection");
    }
    if (size > limit) {
        return;
    }
    // ...
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I get selected data list and structure the data list

How can I convert a structure with a byte slice data type to bytes?

Can I define a structure data type in CMake?

How can I get and remove the first element from a data structure?

Can I get a clean data structure with aggregations

How do I deserialize this type of data structure

how to get data in the structure

How can I push polymorphism into a data structure?

How can I insert data into this structure in BigQuery?

How do I get data on a hierarchical structure?

How to deserialize a JSON to generic objects and get the appropriate type based on the data structure in the JSON?

How can I define a static templated variable without knowing the template data type?

How can I limit a generic data structure to allow only elements of a specific interface?

How can I read data from firebase without knowing id?

How do I get POST data without knowing the id?

How do I subset a list with mixed data type and data structure?

In C++, can I find in a data structure without generating the whole data structure?

Haskell generic data structure

Get the parent/grandparent without knowing the structure?

Why can't I get the article data from this structure?

How can I get WordPress to pull all description metadata fields without knowing their names or length?

how can i change structure of the way i retrieve the data in mysql

BeautifulSoup get data without page structure

How to access a structure member without knowing the name?

What is the best data structure for storing a fixed length collection of named constants that can be used in a switch without explicit casting?

How can I store a data structure such as a Hashmap internally in Android?

How can I replace values in a deeply nested but arbitrary data structure?

How can I have Pandas recognize the structure of my data properly?

How can I access the data in this JSON structure from jQuery?