I have a arraylist(eg.CustInfo) which contains a collection of objects(Cust_details,Cust_Auth) for java classes, which in turn has collection of values for the variables(eg.Cust_name,Cust_addr) defined.
Now my problem is that I have a new class(eg. Priviliged_Cust) which has certain variables whose values have to be taken from the objects added inside the arraylist.
i tried to typecast the arraylist to the new class and get the data using get(index) but it throwed castException.
Please help me with this!!!
Additional Explanation,
First, a query is ran and the result is mapped into the respective variables of the respective class
with the help of setter methods using the instance of an object of that particular class.
then this object is added into Arraylist and the Arraylist is returned.
Second,This happens to a set of queries and the data is mapped to the respective classes and the
object added into the arraylist and the respective arraylists are returned to the respective calling methods.
My Question is, I’ve created a new class which contains the same field as in the previous classes and
it also needed to be mapped to the same variables as the above classes from the arraylist which has the
objects which contains the value of the fields that are needed to be mapped.
Since the queries are returning only the Arraylist, i need to fetch the object from the models and
then i need to match each of the fields respectively.
Hope this information helps.
Don’t use collections to store data that is not related to each other. Use member variables with getters and setters for these values. You can then get whichever value you want by just calling the getter.
If you want to create a new Object that has some values from your existing data, don’t try to cast your data. Just create the new Object and use constructor parameters to give it its values.