java将集合转换为数组(二维数组)
来源:互联网 发布:三菱plc仿真软件汉化 编辑:程序博客网 时间:2024/04/30 06:34
public class ListToArray { /** * 获取对象属性,返回一个字符串数组 * * @param o 对象 * @return String[] 字符串数组 */ private String[] getFiledName(Object o) { try { Field[] fields = o.getClass().getDeclaredFields(); String[] fieldNames = new String[fields.length]; for (int i = 0; i < fields.length; i++) { fieldNames[i] = fields[i].getName(); } return fieldNames; } catch (SecurityException e) { e.printStackTrace(); System.out.println(e.toString()); } return null; } /** * 使用反射根据属性名称获取t属性的get方法 * * @param fieldName 属性名称 * @param o 操作对象 * @return List<Method> get方法 */ private List<Method> getGetField(String[] fieldNames, Object o) { List<Method> methods=new ArrayList<Method>(); for (String fieldName : fieldNames) { String firstLetter = fieldName.substring(0, 1).toUpperCase(); String getter = "get" + firstLetter + fieldName.substring(1); Method method = null; try { method = o.getClass().getMethod(getter, new Class[] {}); } catch (NoSuchMethodException e) { System.out.println("属性不存在"); continue; } //Object value = method.invoke(o, new Object[] {}); methods.add(method); } return methods; } /** * 将list集合转换为二维string数组 * * @param list 要转换的集合 * @return String[][] 返回的sting数组 */ public String[][] listToArrayWay(List list) { Object o=list.get(0); String[] filedNames = getFiledName(o); int filedNum=filedNames.length; int listSize=list.size(); List<Method> methods=getGetField(filedNames, o); String[][] arrs=new String[listSize][filedNum]; int i=0; for (Object object : list) { int j=0; for (Method method : methods) { Object value=null; try { value = method.invoke(object, new Object[] {}); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("属性不存在"+e); } arrs[i][j]=(String) value; j++; } i++; } return arrs; }}
0 0
- java将集合转换为数组(二维数组)
- 将json格式的数组字符串转换为java集合
- toArray():将集合转换为数组
- java字符串转换为二维数组
- 集合转换成二维数组
- 集合转换为数组
- java 将数组转化为集合列表
- js将字符串转换为一维二维数组
- DataTable转换为二维数组
- 二维数组转换为 DataTable
- 将一维数组强制转换为二维数组
- ArrayList<T>数组转换为二维数组
- 快速写入Excel(将数据转换为二维数组,再使用Range方法)
- 将List集合转换List数组,将List数组转换为String数组,将String字符串放到String数组中,将一个数组赋值给另一个数组
- 将arraylist集合转换为数组并打印出来
- Java如何将数组转换为ArrayList(ArrayList和数组间的相互转换)
- List集合转换为数组
- 集合转换为Json数组
- 匿名内部类
- 【SPOJ-NAGAY】Joseph’s Problem【余数求和】【分块】
- 在Eclipse中创建Maven多模块工程的例子
- Wordpress 网站搭建及性能监控方法详解!
- C++类和对象的应用实例——链表
- java将集合转换为数组(二维数组)
- 进程同步,条件变量,pthread_cond_wait,pthread_cond_init,PTHREAD_COND_INITIALIZER
- Ubuntu登陆windows搭建的FTP服务器文件名乱码解决方法
- 关于如果自己一个人负责测试一个app的思考
- Palindrome Number
- PAT--1084 Broken Keyboard
- collectionview resueview 重影的问题
- xib 自定义cell高度
- 在 const 和 non-const 重载的成员函数中避免代码重复