猿问

创建从任何自定义对象类型创建 csv 字符串的方法 - 通用

是否可以创建方法来循环任何自定义对象类型的列表,然后从中构建一个 csv 字符串?


到目前为止,我有一个这样的方法签名:


loopData(List<T> records){


}

然后我想循环对象中的每个字段,而不必指定对象类型。


到目前为止,这就是我所看到的,但它似乎明确说明了对象类型,如 ClassABC 中所示:


ClassABC abc = new ClassABC();//!!explicitly stating custom object type!!!

for (Field field : abc.getClass().getDeclaredFields()) {

    field.setAccessible(true);

    String name = field.getName();

    Object value = field.get(abc);

    System.out.printf("%s: %s%n", name, value);

}

有什么办法可以做到这一点?循环每个字段而不明确说明对象类型?


像这样的东西怎么样:


for(T o : records){

            for(Field field:o.getClass().getDeclaredFields()) {

                field.setAccessible(true);

                String name = field.getName();

               Object value = null;

               try {

                   value = field.get(o);

               } catch (IllegalArgumentException | IllegalAccessException e) {

                   // TODO Auto-generated catch block

                   e.printStackTrace();

               }

               System.out.printf("%s: %s%n", name, value);

            }

}

这个循环是否一致?它会每次都以相同的顺序循环对象字段吗?


慕无忌1623718
浏览 88回答 1
1回答

慕勒3428872

最后,这就是有效的...我可以获取字段名称并将它们发送给 DOMO 以进行创建,如下所示:&nbsp; &nbsp; List<Column> domoColumns = new ArrayList<Column>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(Field field:o.getClass().getDeclaredFields()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String name = field.getName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; domoColumns.add(new Column(ColumnType.STRING,name));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }return domoColumns;然后我像这样创建 CSV:StringBuilder stringBuilder = new StringBuilder();&nbsp; &nbsp; for (T o : records) {&nbsp; &nbsp; &nbsp; &nbsp; int numberOfFields = o.getClass().getDeclaredFields().length;&nbsp; &nbsp; &nbsp; &nbsp; int counter = 1;&nbsp; &nbsp; &nbsp; &nbsp; Field[] f = o.getClass().getDeclaredFields();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < o.getClass().getDeclaredFields().length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f[i].setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object value = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = f[i].get(o);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (counter < numberOfFields) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append("\"").append(value).append("\"").append(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringBuilder.append("\"").append(value).append("\"").append(System.getProperty("line.separator"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalArgumentException | IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO Auto-generated catch block&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答