我有三个数组string[]Type;string[] id;string[] uesername,他们长度都相同,且都为8。他们中的{type[0],id[0],username[0]}{type[1],id[1],username[1]}以此类推直到7都是一组。请问怎么把他们筛选出来放入一个对象。要每个都分开来!求代码!
放进list吧。
追答import java.util.ArrayList;
import java.util.List;
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
List userlist = getUsers(type, id, username);
}
private static List getUsers(String[] type, String[] id, String[] username) {
List list = new ArrayList();
for (int i = 0; i < type.length; i++) {
list.add(new User(type[i], id[i], username[i]));
}
return list;
}
}
class User {
public User(String tpye, String id, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
谢谢!我想问一下,list里面就包含了这几个元素了吧,那么list(0)的时候就是type[0],id[0],username[0]是吧?
追答这样用
list.get(0).getType();
list.get(0).getId();
list.get(0).getUsername();
上面那个数组的
这样用
users[0].getType();
users[0].getId();
users[0].getUsername();
也可以这样用
User u1 = list.get(0);
u1.getTpye();
...
遍历的话可以这样
for(User u : list) {
System.out.println(u.getTpye()+" "+u.getId()+" "+u.getUsername());
}
好的!非常感谢!
本回答被提问者采纳