[英]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.