ArrayList Problem

Hi everyone in the forum,

this is my prob.

i am working with an AbstractList and templates depending on the numbers of the elements of this list, i mean two data or three, and now the view of the model needs to know which are the data types, and i am wondering how it could be done.

AbstractTableListElement<Textends DefaultTableElement>extends ArrayList<T>

The DefaultTableElement class has two fields and its extension add more fields, eg DefaultOptionTableElement has three fields.

Now the point is the view build a table which has an AbstractTableListElement field, but it is neccesary to get how many fields has the list, and that is my big deal. What i knowis that with getClass commadn i will know the class type of the list, an ArrayList, but not what type of elements contains, and i suppose theere should be any method but not found it.

If anyone could give me a hand? Thanks a lot.

[1015 byte] By [patucosa] at [2007-11-26 17:46:31]
# 1

Not possible.

From the API documentation:

Generics are implemented by type erasure: generic type information is present only at compile time, after which it is erased by the compiler.

Can a single instance of AbstractTableListElement contain more than one type of DefaultTableElement? IOW, can you have a DefaultTableElement and a DefaultOptionTableElement in the same AbstractTableListElement? If so, you'll need to use instanceof to determine the type of each individual element in the AbstractTableListElement. If not, you have two options:

1) pass the absolute class object as part of the constructor for AbstractTableListElement and make it accessible with a get method

2) create subclasses of AbstractTableListElement that uses a single generic class without extensions.

Jasprea at 2007-7-9 4:58:43 > top of Java-index,Desktop,Core GUI APIs...
# 2

The Abstract can be just one type of list for instance each time but there is a copule of instances to this abstract (4 classes has a list of abstracts, 2 of them de contains elements of the type DefaultTableElement and the other ones the other type of elements ). the main problem is when i try to instantiate the list at first time it is empty and as it is clear null pointer exception comes out.

My idea was to try to get the element type of a list (initaialized as ArrayList) because the first thing i am doing is defining the data type conatined, and i do not know but suppose there should be difference between the declaration

ArrayList<Integer>()

and

ArrayList<Double>()

but no idea how to differ them.

patucosa at 2007-7-9 4:58:43 > top of Java-index,Desktop,Core GUI APIs...
# 3

sorry, I don't understand what you're saying.

If I understand your first post, you need to know if the AbstractTableListElement instance contains DefaultTableElement or DefaultOptionTableElement, right?

My solution #1 from my reply would then be something like this:

class AbstractTableListElement<T extends DefaultTableElement> extends ArrayList<T> {

Class<T> elementTypeClass;

public AbstractTableListElement(Class<T> elementTypeClass) {

this.elementTypeClass = elementTypeClass;

}

public Class<T> getElementTypeClass() {

return elementTypeClass;

}

}

// then to use it

class Foo {

AbstractTableListElement<DefaultOptionTableElement> list;

public Foo() {

list = new AbstractTableListElement<DefaultOptionTableElement>(

DefaultOptionTableElement.class);

}

}

Jasprea at 2007-7-9 4:58:43 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks man,i did something like your idea and works perfectly with the templates
patucosa at 2007-7-9 4:58:43 > top of Java-index,Desktop,Core GUI APIs...