java怎么创建一个对象来存放数组的值。

我有三个数组string[]Type;string[] id;string[] uesername,他们长度都相同,且都为8。他们中的{type[0],id[0],username[0]}{type[1],id[1],username[1]}以此类推直到7都是一组。请问怎么把他们筛选出来放入一个对象。要每个都分开来!求代码!

第1个回答  2012-09-29
public class Test{
public static void main(String[] args) {
User[] users = new User[8];
Test t = new Test();
users[0] = t.new User("1", "type", "username");
users[1] = t.new User("2", "type", "username");
users[2] = t.new User("3", "type", "username");
users[3] = t.new User("4", "type", "username");
users[4] = t.new User("5", "type", "username");
users[5] = t.new User("6", "type", "username");
users[6] = t.new User("7", "type", "username");
users[7] = t.new User("8", "type", "username");
}

class User{

public User(String id, String type, String username){
this.id = id;
this.type = type;
this.username = username;
}

public String type;
public String id;

public String username;

}
}
第2个回答  2012-09-29
如果是定值非常好弄;
Object [][]obj = new Object [8][3];//如果确认是String ,可以直接定义成String 类型
for(int i = 0; i<8;i++){
obj[i][0] = Type[i];
obj[i][1] = id[i];
obj[i][2] = username[i];
}
//obj里面应该存储的就是你想要的东西了。
第3个回答  2012-09-29
把数字中的每项作为对象中的一个字段
第4个回答  2012-09-29
//新建一个类User用来存放这,三个数据
//结果放到一个User数组中,你看这可以吗?
//还是说要放到一个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];

User[] users = getUsers(type, id, username);
}

private static User[] getUsers(String[] type, String[] id, String[] username) {
User[] users = new User[type.length];
for (int i = 0; i < type.length; i++) {
users[i] = new User(id[i], type[i], username[i]);
}
return users;
}
}

class User {

public User(String id, String tpye, 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吧。

追答

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());

}

追问

好的!非常感谢!

本回答被提问者采纳
相似回答