我有一个字符串数组
adrs[] = {
"BD Jules Ferry",
"Rue de Republique",
"Avenue de Julin"};
我有一个名为“Address”的对象(为了简单起见,省略了 getter setter)
public class Address{
adrsLine1 String;
adrsLine2 String;
adrsLine3 String;
postalCode String;
}
我想通过将数组中的每个字符串分配为 的参数来将其转换adrs[]
为。List<<Address>> addrsList
adrsLine1
Address
一旦转换后,我的每个地址对象addrsList
就会像这样,
addrsList.get(0)
将有 [BD Jules Ferry, null, null, null]
addrsList.get(1)
将有 [Rue de Republique, null, null, null]
addrsList.get(2)
将有 [Avenue de Julin, null, null, null]
我使用Java 8,目前,我用它for loop
来实现这一点,Java 8中有没有捷径或更有效的方法?
示例代码:
private String[] AdrsArray = {"BD Jules Ferry", "Rue de Republique", "Avenue de Julin"};
List<Address> addrsList = new ArrayList<>();
for (String adrsLine : AdrsArray){
Address address = new ReturnAddress();
address.setAdrsLine1(adrsLine);
addrsList.add(address);
}
输出:
Address address = addrsList.get(0);
address.getAdrsLine1() //should have "BD Jules Ferry"
address.getAdrsLine2() //should have null
address.getAdrsLine3() //should have null
address.getAdrsPostalCode() //should have null
萧十郎
湖上湖
呼如林
相关分类