[英]Creating objects dynamically using a list of Strings?
I am trying to create objects
by using a list of Strings
that will populate their fields. 我试图通过使用将填充其字段的
Strings
列表来创建objects
。 For example I have the list of strings, Note that the values repeat after every 3. ie id, name , address
. 例如,我有一个字符串列表,请注意,值每隔3.后重复一次,即
id, name , address
。
List<String> myList = "Id1", "name1", "address1", "Id2", "name2", "address2";
I would like to dynamically create a number of Person Objects (shown below) using this list 我想使用此列表动态创建许多个人对象(如下所示)
Person object:
public class Person {
private String id;
private String name;
private String address;
public Person() {
}
public Person(String id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}
//standard getters and setters
}
What I want to do is have a method that will take the list of strings as an input and then create the objects dynamically. 我想要做的是有一个方法,该方法将字符串列表作为输入,然后动态创建对象。 How could I best do this?
我怎样才能最好地做到这一点?
I know that I could do the following if I knew that I was definitely populating 2 objects, but the problem is that there may be more or less. 我知道,如果我知道我肯定是在填充2个对象,则可以执行以下操作,但是问题是可能存在更多或更少。
public List<Person> createObjectsFromStringList(List<String> list){
List<person> personList = new Arraylist<>();
Person person1 = new Person(list.get(0), list.get(1), list.get(2));
Person person2 = new Person(list.get(3), list.get(4), list.get(5));
personList.add(person1);
personList.add(person2);
return personList;
}
A simple for
loop can do the work: 一个简单的
for
循环可以完成工作:
public List<Person> createObjectsFromStringList(List<String> list) {
List<person> personList = new Arraylist<>();
//We use < size-2 here because we access 2 indeces ahead of x in this loop
for(int x=0; x<list.size()-2; x+=3) {
personList.add(new Person(list.get(x), list.get(x+1), list.get(x+2));
}
return personList;
}
At first glance, I feel like having the different field values across one List
is a sign of a poor code structure, but maybe you're already stuck with this List
as-is. 乍一看,我觉得在一个
List
上使用不同的字段值表明代码结构不佳,但也许您已经按原样使用了此List
。
Edit: 编辑:
Now let's suppose you want a partial Person
based on the number of remaining elements. 现在,假设您希望基于剩余元素的数量来获得部分
Person
。 Supposing they are still in the same order, you could modify this method to check the validity of the current index for each field: 假设它们仍然是相同的顺序,则可以修改此方法以检查每个字段的当前索引的有效性:
public List<Person> createObjectsFromStringList(List<String> list) {
List<person> personList = new Arraylist<>();
int size = list.size();
//Now we remove the "-2" from size check because we will handle this ourselves
for(int x=0; x<size; x+=3) {
String id = list.get(x); //Obviously valid
String name = x+1 < size? list.get(x+1) : null;
String address = x+2 < size? list.get(x+2) : null;
personList.add(new Person(id, name, address);
}
return personList;
}
We're using the ternary operation ? ... : null
我们正在使用三元运算
? ... : null
? ... : null
here, so if we run out of elements we set the associated Person
field to null
instead of using an out-of-bounds index. ? ... : null
此处为? ... : null
,因此如果元素用完,则将关联的Person
字段设置为null
而不是使用越界索引。
You can use recursion 您可以使用递归
public List<Person> createObjectsFromStringList(List<String> list){
List<person> personList = new Arraylist<>();
for(int i=0; i<list.size(); i++){
personList.add(new Person(list(i),list(i+1),list(i+2)));
i+=2;
}
return personList;
}
Notice that restructuring your list would me much better. 请注意,重组您的列表对我会更好。 make it like this:
使它像这样:
List<String> myList = "Id1_name1_address1", "Id2_name2_address2";
Or even use different lists (it is much better). 甚至可以使用其他列表(效果更好)。 If you change your list structure as above then change the code to this :
如果您如上所述更改列表结构,则将代码更改为此:
public List<Person> createObjectsFromStringList(List<String> list){
List<person> personList = new Arraylist<>();
for(int i=0; i<list.size(); i++){
String[] info= list(i).split("_"); // this will give u a 3element array of yout info IdX nameX addressX
personList.add(new Person(info(0),info(1),info(2)));
}
return personList;
As you want to access your elements sequentially you should use java.util.LinkedList
in such loop 当您要顺序访问元素时,应在此类循环中使用
java.util.LinkedList
for(true)
if(linkedList.size()>=3){
Person person= new
Person(linkedList.removeFirst(),linkedList.removeFirst(),linkedList.removeFirst());
personList.add(person);
}
else break;
But ArrayList
and its get
method is good for random access by index which is not your case 但是
ArrayList
及其get
方法非常适合按索引随机访问,这不是您的情况
If you use java 8 you can try something like this: 如果您使用Java 8,则可以尝试如下操作:
public List<Person> createObjectsFromStringList(List<String> list) {
//partition by 3 and list.size.
Map<Integer,List<Integer>> map = IntStream
.range(0,list.size())
.boxed()
.collect(Collectors.groupingBy(e->(e)/3));
List<Person> personList = new ArrayList<>();
map.entrySet().forEach(e->{
List<String> per= e.getValue();
Person p = new Person(per.get(0),per.get(1),per.get(2));
personList.add(p);
});
return personList;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.