简体   繁体   中英

How to convert an Object[] type to Userdefined array type

I am creating the Leg Objects this way

List<Leg> legs = new ArrayList<Leg>(legdata.length);

I need to pass this legs to an method with the below signature as shown :

 public static String getStrategy(Leg[] leg)

when i did the below way i am getting an error .

String resultData = CMPUtil.getStrategy(legs.toArray());

Also tried this way

Leg les[] = (Leg)legs.toArray();  ( It says cannot cast from Object to Leg)

could anybody please let me know , how to resolve this ??

将正确类型的数组传递给List.toArray另一个重载

Leg[] legsArray = legs.toArray(new Leg[0]); // Or new Leg[legs.size()]

尝试

String resultData = CMPUtil.getStrategy((Leg [])legs.toArray(new Leg[legs.size()]));

Leg les [] = Legs.toArray(new Leg [legs.length]);

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