简体   繁体   中英

How to get the contents of an arraylist into an object?

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.

I suggest you use generics to check the collections types.

List<CustInfo> custList = ...
CustInfo ci = custList.get(n); // no cast required.
ArrayList<Priviliged_Cust> CustInfo = new ArrayList<Priviliged_Cust>();

使用泛型

Firstly, I didn't fully understand how you have structured your code, some code examples would help us understand better.

Saying that I guess your problem is that you have anObject and anotherObject and you want anotherObject to have some of the same properties as anObject . There are a few ways to solve this.

Two simple ways include:

  1. Have anotherObject extend anObject (Inheritance)

  2. Have anotherObject contain anObject (Composition)

difficult to answer without seeing the code, but i guess you want to build an object with data distributed across fields in different objects in the arraylist. you can use dozer or similar tools for this purpose (object to object mapping). define the mappings and then iterate over the list, end of iteration you should have the desired information captured on your object.

Like some others have already posted, it's a little hard to decipher what exactly you want answered.

My stab at your question is that you have an ArrayList containing class objects, and you want to transpose some object details from one of them to another class (called Priviliged_Cust)

Therefore:

<BaseClass> temp = <ArrayList-name>.get(index);    // puts the desired object into a new reference 

Priviliged_Cust newCust = new Priviliged_Cust (temp); // copies the details of the object referred by temp 
                                                  //and instantiates a new Priviliged_Cust from it.

This will only work if you have a constructor in your Priviliged_Cust Class that accepts a object as a parameter and then copies the details of the base into the new class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM