[英]Map array to object
我有一个数组定义如下:
String [] source = {"26", "Tom", "foo", ...};
和一个Person
:
public class Person{
private String age;
private String name;
private String print;
private String ......;//the same type and order and number of source
public Person() {
}
//full construtors
public Person(String age, String name, String print,String ....) {
this.age = age;
this.name = name;
this.print = print;
//....
}
/* setters & getters */
}
如何将这些值映射到Person
实例?
这是我真正的编码
public static List<BasicalVo> readObject(String path) throws IOException, NoSuchMethodException {
InputStreamReader fReader = new InputStreamReader(new FileInputStream(path),"gb2312");
BufferedReader bufferedReader = new BufferedReader(fReader);
String currentLine;
String[] temp;
List<BasicalVo> basicalVoList= new ArrayList<BasicalVo>();
while ((currentLine = bufferedReader.readLine()) != null) {
temp = currentLine.split(",");//I get the Array
for (int i = 0; i < temp.length; i++) {
//I don't know hot to translate to BasicalVo .
BasicalVo vo = new BasicalVo();
basicalVoList.add(vo);
}
}
return basicalVoList;
}
如果source
只包含一个人的数据,那么您可以这样做:
Person p = new Person(source[0], source[1], source[2] ...);
如果数组太短,将抛出ArrayIndexOutOfBoundsException 。
一世。
如果数组只包含一个Person
,则只需要创建如下的实例:
String[] source = new String[]{"26", "tom", "xx", "....."};
Person p = new Person(source[0], source[1], source[2], source[3],...);
因为您将知道构造函数中有多少参数,所以如果数组构建良好,您将不会有ArrayIndexOutOfBoundsException
II。
假设你只有3个属性,如果数组是这样的话,你就可以这样做:
String[] source = new String[]{"26", "tom", "xx", "22", "john", "yy"};
ArrayList<Person> list = new ArrayList<>()
for (int i = 0; i < source.length; i += 3) {
list.add(new Person(source[i], source[i + 1], source[i + 2]));
}
III。
如果您有多个字段,最好这样做:
public Person(String[]source) {
this.age = source[0];
this.name = source[1];
this.print = source[2];
//....
}
因为它不会使您在循环中从数据中读取的代码附加费用,并使您更容易完成您的工作,事实上这并不难,因为在每种情况下,如果您有20个字段,那么'我必须分配这20个属性
IV。
或者使用工厂方法的最后一个命题:
public static Person createPersoneFromArray(String[] array) {
Person p = new Person();
p.setAge(array[0]);
p.setName(array[1]);
//...
return p;
}
并在主要方法:
Person p = Person.createPersoneFromArray(source);
你还可以在BasicalVo类中添加另一个构造函数,它将String []作为输入:
public BasicalVo(String [] input) {
this.age = input[0];
this.name = input[1];
this.print = input[2];
//....
}
然后你可以在你的main中调用如下,而无需额外的for循环
....
temp = currentLine.split(",");
BasicalVo vo = new BasicalVo(temp);
basicalVoList.add(vo);
....
在这个特定的情况下,我认为你最好的选择是使用反射。 Reflection是一组类和接口,允许您在执行时调用不同的方法。 例如:
String [] source = { "26", "tom", "xx", ... };
Constructor constructor = Person.class.getConstructors()[0]
constructor.newInstance(source)
请注意,此示例仅起作用,因为您只有一个构造函数,因此Person.class.getConstructors()[0]
返回所需的构造函数。 你可以尝试使用Person.class.getConstructors(Class<?>...)
来获取特定的构造函数,在这种情况下,您需要将参数类型的数组作为参数传递。
您可以使用OpenCSV
CSVReader csvReader = new CSVReader(new FileReader("people.csv"),',');
ColumnPositionMappingStrategy mappingStrategy = new ColumnPositionMappingStrategy();
mappingStrategy.setType(Person.class);
String[] columns = new String[]{"age","name","print"};
mappingStrategy.setColumnMapping(columns);
CsvToBean ctb = new CsvToBean();
List personList = ctb.parse(mappingStrategy, csvReader);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.