java List Object[] 转换成一个List T 工具类

List 转换成一个List 工具类,代码如下:

ObjectConvertUtils.java文件:

package com.xm.utils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

/**
 * List转换成一个List工具类
 * @author ouyangjun
 */
public class ObjectConvertUtils {
	
	/**
	 * 把一个List转换成一个List
	 * @param list
	 * @param clz
	 * @return
	 * @throws Exception
	 */
	public static  List objectToBean(List objList, Class clz) throws Exception{
		if(objList==null || objList.size()==0) {
			return null;
		}
		
		Class[] cz = null;
		Constructor[] cons = clz.getConstructors();
		for(Constructor ct : cons) {
			Class[] clazz = ct.getParameterTypes();
			if(objList.get(0).length == clazz.length) {
				cz = clazz;
				break;
			}
		}
		
		List list = new ArrayList();
		for(Object[] obj : objList) {
			Constructor cr = clz.getConstructor(cz);
			list.add(cr.newInstance(obj));
		}
		return list;
	}
	
}

PhoneVO.java文件:

package com.xm.bean;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * vo
 * @author ouyangjun
 */
public class PhoneVO implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public PhoneVO() {}
	
	/**
	 * 这个构造器赋值时使用
	 * 注意: 赋值时,参数类型一定要对应
	 * @param plate
	 * @param number
	 * @param memory
	 * @param size
	 */
	public PhoneVO(String plate, BigDecimal number, double memory, double size) {
		super();
		this.plate = plate;
		this.number = String.valueOf(number);
		this.memory = String.valueOf(memory);
		this.size = String.valueOf(size);
	}

	private String plate;
	private String number;
	private String memory;
	private String size;

	public String getPlate() {
		return plate;
	}

	public void setPlate(String plate) {
		this.plate = plate;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getMemory() {
		return memory;
	}

	public void setMemory(String memory) {
		this.memory = memory;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}
	
}

ObjectToBeanTest.java文件:

package com.xm.test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.xm.bean.PhoneVO;
import com.xm.utils.ObjectConvertUtils;

/**
 * 测试类
 * @author ouyangjun
 */
public class ObjectToBeanTest {

	public static void main(String[] args) throws Exception {
		
		// 初始化数据
		Object[] obj = new Object[4];
		obj[0] = "hw";
		obj[1] = BigDecimal.valueOf(13699999999L);
		obj[2] = 4.7;
		obj[3] = 5.0;
		
		Object[] obj1 = new Object[4];
		obj1[0] = "apple";
		obj1[1] = BigDecimal.valueOf(13611111111L);
		obj1[2] = 4.8;
		obj1[3] = 5.7;
		
		List objList = new ArrayList();
		objList.add(obj);
		objList.add(obj1);
		
		// 工具类
		List list = ObjectConvertUtils.objectToBean(objList, PhoneVO.class);
		for(PhoneVO phone : list) {
			System.out.println(phone.getPlate() + "\t" + phone.getNumber() +
					"\t" + phone.getMemory() + "\t" + phone.getSize());
		}
		
	}

}

执行测试结果如下:

java List Object[] 转换成一个List T 工具类_第1张图片

本文说明:该文章属于原创,如需转载,请标明文章转载来源

你可能感兴趣的:(Java)