场景:
    将Object数组转换为指定类型数组
1.转换函数
public static <T> T[] convertArray(Class<T> targetType, Object[]
arrayObjects) { if (targetType == null) { return (T[]) arrayObjects; } if
(arrayObjects == null) { return null; } T[] targetArray = (T[])
Array.newInstance(targetType, arrayObjects.length); try {
System.arraycopy(arrayObjects, 0, targetArray, 0, arrayObjects.length); } catch
(ArrayStoreException e) { e.printStackTrace(); } return targetArray; }
2.测试main函数
public static void main(String []args){ GirlVo girlVo =new GirlVo();
girlVo.setGirlId("08123"); girlVo.setGirlName("张一"); GirlVo girlVo2 =new
GirlVo(); girlVo2.setGirlId("08456"); girlVo2.setGirlName("张二"); Object[]
arrayObjects2 = new Object[2]; arrayObjects2[0] = girlVo; arrayObjects2[1] =
girlVo2; GirlVo[] arrayObjects3 ; arrayObjects3 =
convertArray(GirlVo.class,arrayObjects2); for(int
i=0;i<arrayObjects3.length;i++){ System.out.println("输出GirlVo:
"+arrayObjects3[i].toString()); } }
3.实体类
public class GirlVo implements Serializable { private static final long
serialVersionUID = -8545755852948661858L; /**ID号*/ String girlId; /**姓名*/
String girlName; /**年龄*/ long age; /**身高*/ double height; /**罩杯*/ String
cupSize; public String getGirlId() { return girlId; } public void
setGirlId(String girlId) { this.girlId = girlId; } public String getGirlName()
{ return girlName; } public void setGirlName(String girlName) { this.girlName =
girlName; } public long getAge() { return age; } public void setAge(long age) {
this.age = age; } public double getHeight() { return height; } public void
setHeight(double height) { this.height = height; } public String getCupSize() {
return cupSize; } public void setCupSize(String cupSize) { this.cupSize =
cupSize; } @Override public String toString() { return "GirlVo [girlId=" +
girlId +",girlName=" + girlName +",age=" + age +",height=" + height
+",cupSize=" + cupSize +"]"; } }
以上,TKS.

技术
下载桌面版
GitHub
Gitee
SourceForge
百度网盘(提取码:draw)
云服务器优惠
华为云优惠券
腾讯云优惠券
阿里云优惠券
Vultr优惠券
站点信息
问题反馈
邮箱:[email protected]
吐槽一下
QQ群:766591547
关注微信