我有一個數組定義如下:地圖陣列反對
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;
}
如果順序是決定的(例如'age'是第一個),那麼只需傳入'Str ing []'然後遍歷其元素 –
你知道編譯時的參數個數嗎?或者他們可能有所不同 – gmanjon
數組的參數與對象的字段的順序和類型相同 – kevin